Skip to content

Commit d9acae5

Browse files
authored
Merge pull request keithamus#2 from keithamus/add-some-github-workflows-and-improve-package-json
add some github workflows and improve package.json
2 parents e43c01f + 104e304 commit d9acae5

File tree

8 files changed

+429
-4706
lines changed

8 files changed

+429
-4706
lines changed

.github/workflows/publish.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Publish
2+
3+
on:
4+
release:
5+
types: [created]
6+
7+
jobs:
8+
publish-npm:
9+
name: Publish to npm
10+
runs-on: ubuntu-latest
11+
permissions:
12+
contents: read
13+
id-token: write
14+
steps:
15+
- uses: actions/checkout@v4
16+
- uses: actions/setup-node@v4
17+
with:
18+
node-version: 22
19+
registry-url: https://registry.npmjs.org/
20+
cache: npm
21+
- run: npm install -g npm@latest
22+
- run: npm i
23+
- run: npm test
24+
- run: npm run minify
25+
- run: npm version ${TAG_NAME} --git-tag-version=false
26+
env:
27+
TAG_NAME: ${{ github.event.release.tag_name }}
28+
- run: npm whoami; npm --ignore-scripts publish --provenance --access public
29+
env:
30+
NODE_AUTH_TOKEN: ${{secrets.npm_token}}

.github/workflows/test.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Node CI
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
- name: Use Node.js 22.x
15+
uses: actions/setup-node@v4
16+
with:
17+
node-version: 22.x
18+
- name: npm install, build, and test
19+
run: |
20+
npm it
21+
env:
22+
CI: true

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

index-fn.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export declare function isSupported(): boolean;
2+
export declare function isPolyfilled(): boolean;
3+
export declare function apply(): void;

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import { apply, isSupported } from './observable.js';
2+
if (!isSupported()) apply();

observable.js

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -306,10 +306,10 @@ const [Observable, Subscriber] = (() => {
306306
// 5.1.3 Let iterator be iteratorRecord’s [[Value]].
307307
const iterator = value[Symbol.iterator]();
308308
// 5.1.4 Repeat:
309-
while(true) {
309+
while (true) {
310310
const nextRecord = iterator.next();
311311
// 5.1.5. If iterator’s [[Done]] is true, then:
312-
if(nextRecord.done) {
312+
if (nextRecord.done) {
313313
break;
314314
}
315315
// 5.1.5.2 Let nextRecord be IteratorStepValue(iterator).
@@ -465,7 +465,7 @@ const [Observable, Subscriber] = (() => {
465465
inspect(inspectorUnion = {}) {
466466
const sourceObservable = this;
467467

468-
return new Observable(subscriber => {
468+
return new Observable((subscriber) => {
469469
// 1: Let subscribe callback be a `VoidFunction`-or-null, initially null.
470470
let subscribeCallback = null;
471471
// 2: Let next callback be a `ObservableSubscriptionCallback`-or-null, initially null.
@@ -478,22 +478,24 @@ const [Observable, Subscriber] = (() => {
478478
let abortCallback = null;
479479

480480
// 6: Process inspectorUnion as follows:
481-
if (typeof inspectorUnion === 'function') {
481+
if (typeof inspectorUnion === "function") {
482482
// If inspectorUnion is an `ObservableSubscriptionCallback`
483483
// 6.1: Set next callback to inspectorUnion.
484484
nextCallback = inspectorUnion;
485-
} else if (inspectorUnion && typeof inspectorUnion === 'object') {
485+
} else if (inspectorUnion && typeof inspectorUnion === "object") {
486486
// If inspectorUnion is an `ObservableInspector`
487487
// 6.1: If `subscribe` exists in inspectorUnion, then set subscribe callback to it.
488-
if ('subscribe' in inspectorUnion) subscribeCallback = inspectorUnion.subscribe;
488+
if ("subscribe" in inspectorUnion)
489+
subscribeCallback = inspectorUnion.subscribe;
489490
// 6.2: If `next` exists in inspectorUnion, then set next callback to it.
490-
if ('next' in inspectorUnion) nextCallback = inspectorUnion.next;
491+
if ("next" in inspectorUnion) nextCallback = inspectorUnion.next;
491492
// 6.3: If `error` exists in inspectorUnion, then set error callback to it.
492-
if ('error' in inspectorUnion) errorCallback = inspectorUnion.error;
493+
if ("error" in inspectorUnion) errorCallback = inspectorUnion.error;
493494
// 6.4: If `complete` exists in inspectorUnion, then set complete callback to it.
494-
if ('complete' in inspectorUnion) completeCallback = inspectorUnion.complete;
495+
if ("complete" in inspectorUnion)
496+
completeCallback = inspectorUnion.complete;
495497
// 6.5: If `abort` exists in inspectorUnion, then set abort callback to it.
496-
if ('abort' in inspectorUnion) abortCallback = inspectorUnion.abort;
498+
if ("abort" in inspectorUnion) abortCallback = inspectorUnion.abort;
497499
}
498500

499501
// 7: Let sourceObservable be this.
@@ -515,15 +517,19 @@ const [Observable, Subscriber] = (() => {
515517

516518
// 8.2: If abort callback is not null, then add the following abort algorithm to subscriber’s subscription controller’s signal:
517519
if (abortCallback !== null) {
518-
subscriber.signal.addEventListener('abort', () => {
519-
// 8.2.1: Invoke abort callback with subscriber’s subscription controller’s signal’s abort reason.
520-
try {
521-
abortCallback(subscriber.signal.reason);
522-
} catch (e) {
523-
// If an exception E was thrown, then report the exception E.
524-
console.error(e);
525-
}
526-
}, { once: true });
520+
subscriber.signal.addEventListener(
521+
"abort",
522+
() => {
523+
// 8.2.1: Invoke abort callback with subscriber’s subscription controller’s signal’s abort reason.
524+
try {
525+
abortCallback(subscriber.signal.reason);
526+
} catch (e) {
527+
// If an exception E was thrown, then report the exception E.
528+
console.error(e);
529+
}
530+
},
531+
{ once: true },
532+
);
527533
}
528534

529535
// 8.3: Let sourceObserver be a new internal observer, initialized as follows:
@@ -538,7 +544,7 @@ const [Observable, Subscriber] = (() => {
538544
// If an exception E was thrown, then:
539545
// 8.3.next.1.1: Remove abort callback from subscriber’s subscription controller’s signal.
540546
if (abortCallback !== null) {
541-
subscriber.signal.removeEventListener('abort', abortCallback);
547+
subscriber.signal.removeEventListener("abort", abortCallback);
542548
}
543549
// 8.3.next.1.2: Run subscriber’s `error()` method, given E, and abort these steps.
544550
subscriber.error(e);
@@ -552,7 +558,7 @@ const [Observable, Subscriber] = (() => {
552558
// error steps
553559
// 8.3.error.1: Remove abort callback from subscriber’s subscription controller’s signal.
554560
if (abortCallback !== null) {
555-
subscriber.signal.removeEventListener('abort', abortCallback);
561+
subscriber.signal.removeEventListener("abort", abortCallback);
556562
}
557563
// 8.3.error.2: If error callback is not null, then invoke error callback given the passed in error.
558564
if (errorCallback !== null) {
@@ -571,7 +577,7 @@ const [Observable, Subscriber] = (() => {
571577
// complete steps
572578
// 8.3.complete.1: Remove abort callback from subscriber’s subscription controller’s signal.
573579
if (abortCallback !== null) {
574-
subscriber.signal.removeEventListener('abort', abortCallback);
580+
subscriber.signal.removeEventListener("abort", abortCallback);
575581
}
576582
// 8.3.complete.2: If complete callback is not null, then invoke complete callback.
577583
if (completeCallback !== null) {
@@ -585,15 +591,15 @@ const [Observable, Subscriber] = (() => {
585591
}
586592
// 8.3.complete.3: Run subscriber’s `complete()` method.
587593
subscriber.complete();
588-
}
594+
},
589595
};
590596

591597
// 8.4: Let options be a new `SubscribeOptions` whose `signal` is subscriber’s subscription controller’s signal.
592598
const options = { signal: subscriber.signal };
593599
// 8.5: Subscribe to sourceObservable given sourceObserver and options.
594600
sourceObservable.subscribe(sourceObserver, options);
595601
});
596-
};
602+
}
597603

598604
// https://wicg.github.io/observable/#dom-observable-filter
599605
filter(predicate) {
@@ -1443,9 +1449,11 @@ const [Observable, Subscriber] = (() => {
14431449
})();
14441450

14451451
function isSupported() {
1446-
if (typeof globalThis.Observable !== "undefined") {
1447-
return;
1448-
}
1452+
return typeof globalThis.Observable === "function";
1453+
}
1454+
1455+
function isPolyfilled() {
1456+
return globalThis.Observable === Observable;
14491457
}
14501458

14511459
function apply() {
@@ -1462,7 +1470,7 @@ function apply() {
14621470
},
14631471
});
14641472

1465-
EventTarget.prototype.when = function(type, options = {}) {
1473+
EventTarget.prototype.when = function (type, options = {}) {
14661474
// Step 1: If this’s relevant global object is a Window object, and its associated Document is not fully active, then return.
14671475
if (globalThis.Window && this instanceof Window && !document?.isConnected) {
14681476
return; // Early return if Document isn’t fully active
@@ -1472,7 +1480,7 @@ function apply() {
14721480
const eventTarget = this;
14731481

14741482
// Step 3: Let observable be a new Observable, initialized with a subscribe callback.
1475-
return new Observable(subscriber => {
1483+
return new Observable((subscriber) => {
14761484
// Step 3.1: If event target is null, abort these steps.
14771485
if (!eventTarget) return;
14781486

@@ -1486,7 +1494,7 @@ function apply() {
14861494
// - passive: options.passive (default undefined)
14871495
// - once: false (explicitly set per spec)
14881496
// - signal: subscriber.signal (for aborting the subscription)
1489-
const listener = event => {
1497+
const listener = (event) => {
14901498
// Observable event listener invoke algorithm: Run subscriber’s next() method with event.
14911499
subscriber.next(event);
14921500
};
@@ -1495,11 +1503,10 @@ function apply() {
14951503
capture: options.capture || false,
14961504
passive: options.passive,
14971505
once: false, // Explicitly false as required by spec
1498-
signal: subscriber.signal
1506+
signal: subscriber.signal,
14991507
});
15001508
});
15011509
};
15021510
}
15031511

1504-
export { Observable, Subscriber, isSupported, apply };
1505-
1512+
export { Observable, Subscriber, isPolyfilled, isSupported, apply };

0 commit comments

Comments
 (0)