@player-ui/react-subscribe 0.0.1-next.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs.js +130 -0
- package/dist/index.d.ts +47 -0
- package/dist/index.esm.js +121 -0
- package/package.json +19 -0
- package/src/index.tsx +177 -0
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var React = require('react');
|
|
6
|
+
|
|
7
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
8
|
+
|
|
9
|
+
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
|
|
10
|
+
|
|
11
|
+
var __async = (__this, __arguments, generator) => {
|
|
12
|
+
return new Promise((resolve, reject) => {
|
|
13
|
+
var fulfilled = (value) => {
|
|
14
|
+
try {
|
|
15
|
+
step(generator.next(value));
|
|
16
|
+
} catch (e) {
|
|
17
|
+
reject(e);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
var rejected = (value) => {
|
|
21
|
+
try {
|
|
22
|
+
step(generator.throw(value));
|
|
23
|
+
} catch (e) {
|
|
24
|
+
reject(e);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
28
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
29
|
+
});
|
|
30
|
+
};
|
|
31
|
+
function deferred() {
|
|
32
|
+
let resolve = () => void 0;
|
|
33
|
+
let reject = () => void 0;
|
|
34
|
+
let status = "pending";
|
|
35
|
+
const promise = new Promise((res, rej) => {
|
|
36
|
+
resolve = (a) => {
|
|
37
|
+
status = "success";
|
|
38
|
+
const resolveFunc = res;
|
|
39
|
+
resolveFunc(a);
|
|
40
|
+
};
|
|
41
|
+
reject = (error) => {
|
|
42
|
+
status = "failure";
|
|
43
|
+
rej(error);
|
|
44
|
+
};
|
|
45
|
+
});
|
|
46
|
+
return {
|
|
47
|
+
resolve,
|
|
48
|
+
status,
|
|
49
|
+
reject,
|
|
50
|
+
promise
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
const NOT_CALLED = Symbol("Subscribe -- Empty Value");
|
|
54
|
+
class Subscribe {
|
|
55
|
+
constructor() {
|
|
56
|
+
this.callbacks = new Map();
|
|
57
|
+
this.deferredResult = deferred();
|
|
58
|
+
this.lastValue = NOT_CALLED;
|
|
59
|
+
this.resetDeferred = null;
|
|
60
|
+
this.publish = this.publish.bind(this);
|
|
61
|
+
this.add = this.add.bind(this);
|
|
62
|
+
this.remove = this.remove.bind(this);
|
|
63
|
+
}
|
|
64
|
+
publish(val) {
|
|
65
|
+
return __async(this, null, function* () {
|
|
66
|
+
var _a;
|
|
67
|
+
yield (_a = this.resetDeferred) == null ? void 0 : _a.promise;
|
|
68
|
+
this.lastValue = val;
|
|
69
|
+
this.deferredResult.resolve(val);
|
|
70
|
+
this.callbacks.forEach((c) => c(val));
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
add(callback, options) {
|
|
74
|
+
const id = this.callbacks.size;
|
|
75
|
+
this.callbacks.set(id, callback);
|
|
76
|
+
if (this.lastValue !== NOT_CALLED && (options == null ? void 0 : options.initializeWithPreviousValue) === true) {
|
|
77
|
+
callback(this.lastValue);
|
|
78
|
+
}
|
|
79
|
+
return id;
|
|
80
|
+
}
|
|
81
|
+
remove(id) {
|
|
82
|
+
this.callbacks.delete(id);
|
|
83
|
+
}
|
|
84
|
+
reset(promise) {
|
|
85
|
+
return __async(this, null, function* () {
|
|
86
|
+
var _a;
|
|
87
|
+
if (promise) {
|
|
88
|
+
this.resetDeferred = deferred();
|
|
89
|
+
yield promise;
|
|
90
|
+
}
|
|
91
|
+
if (this.lastValue !== NOT_CALLED) {
|
|
92
|
+
this.deferredResult = deferred();
|
|
93
|
+
}
|
|
94
|
+
this.lastValue = NOT_CALLED;
|
|
95
|
+
this.callbacks.forEach((c) => c(void 0));
|
|
96
|
+
(_a = this.resetDeferred) == null ? void 0 : _a.resolve();
|
|
97
|
+
this.resetDeferred = null;
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
suspend() {
|
|
101
|
+
if (this.lastValue === NOT_CALLED) {
|
|
102
|
+
throw this.deferredResult.promise;
|
|
103
|
+
}
|
|
104
|
+
return this.lastValue;
|
|
105
|
+
}
|
|
106
|
+
get() {
|
|
107
|
+
if (this.lastValue === NOT_CALLED) {
|
|
108
|
+
return void 0;
|
|
109
|
+
}
|
|
110
|
+
return this.lastValue;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
function useSubscribedState(subscriber) {
|
|
114
|
+
const [state, setState] = React__default["default"].useState(subscriber.get());
|
|
115
|
+
React__default["default"].useEffect(() => {
|
|
116
|
+
const id = subscriber.add((resp) => {
|
|
117
|
+
setState(resp);
|
|
118
|
+
}, {
|
|
119
|
+
initializeWithPreviousValue: true
|
|
120
|
+
});
|
|
121
|
+
return () => {
|
|
122
|
+
subscriber.remove(id);
|
|
123
|
+
};
|
|
124
|
+
}, [subscriber]);
|
|
125
|
+
return state;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
exports.Subscribe = Subscribe;
|
|
129
|
+
exports.useSubscribedState = useSubscribedState;
|
|
130
|
+
//# sourceMappingURL=index.cjs.js.map
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
declare type SubscribeID = number;
|
|
2
|
+
/**
|
|
3
|
+
* A pub-sub module that works across the React bridge
|
|
4
|
+
*/
|
|
5
|
+
declare class Subscribe<T> {
|
|
6
|
+
private callbacks;
|
|
7
|
+
private deferredResult;
|
|
8
|
+
private lastValue;
|
|
9
|
+
private resetDeferred;
|
|
10
|
+
constructor();
|
|
11
|
+
/**
|
|
12
|
+
* Trigger the subscriptions using the provided value
|
|
13
|
+
* if there is a reset in progress, wait for it before publishing a new value.
|
|
14
|
+
*/
|
|
15
|
+
publish(val: T): Promise<void>;
|
|
16
|
+
/**
|
|
17
|
+
* Subscribe to updates
|
|
18
|
+
*/
|
|
19
|
+
add(callback: (arg: T | undefined) => void, options?: {
|
|
20
|
+
/** Use the last updated value for this subscription to immediately trigger the onSet callback */
|
|
21
|
+
initializeWithPreviousValue?: boolean;
|
|
22
|
+
}): SubscribeID;
|
|
23
|
+
/**
|
|
24
|
+
* Remove any updates from the given listener
|
|
25
|
+
*/
|
|
26
|
+
remove(id: SubscribeID): void;
|
|
27
|
+
/**
|
|
28
|
+
* Reset the state of the listener
|
|
29
|
+
* Passing in a promise will defer resetting the view until the promise is resolved
|
|
30
|
+
*/
|
|
31
|
+
reset(promise?: Promise<void>): Promise<void>;
|
|
32
|
+
/**
|
|
33
|
+
* _Throws_ a promise if the value is still pending
|
|
34
|
+
* Otherwise returns it
|
|
35
|
+
*/
|
|
36
|
+
suspend(): T;
|
|
37
|
+
/** Get the current value of the subscription */
|
|
38
|
+
get(): T | undefined;
|
|
39
|
+
}
|
|
40
|
+
interface SubscribedStateHookOptions {
|
|
41
|
+
/** if the state should trigger suspense when waiting to resolve */
|
|
42
|
+
suspend?: boolean;
|
|
43
|
+
}
|
|
44
|
+
/** Subscribe to a state change event in a react component */
|
|
45
|
+
declare function useSubscribedState<T>(subscriber: Subscribe<T>): T | undefined;
|
|
46
|
+
|
|
47
|
+
export { Subscribe, SubscribeID, SubscribedStateHookOptions, useSubscribedState };
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
|
|
3
|
+
var __async = (__this, __arguments, generator) => {
|
|
4
|
+
return new Promise((resolve, reject) => {
|
|
5
|
+
var fulfilled = (value) => {
|
|
6
|
+
try {
|
|
7
|
+
step(generator.next(value));
|
|
8
|
+
} catch (e) {
|
|
9
|
+
reject(e);
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
var rejected = (value) => {
|
|
13
|
+
try {
|
|
14
|
+
step(generator.throw(value));
|
|
15
|
+
} catch (e) {
|
|
16
|
+
reject(e);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
20
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
function deferred() {
|
|
24
|
+
let resolve = () => void 0;
|
|
25
|
+
let reject = () => void 0;
|
|
26
|
+
let status = "pending";
|
|
27
|
+
const promise = new Promise((res, rej) => {
|
|
28
|
+
resolve = (a) => {
|
|
29
|
+
status = "success";
|
|
30
|
+
const resolveFunc = res;
|
|
31
|
+
resolveFunc(a);
|
|
32
|
+
};
|
|
33
|
+
reject = (error) => {
|
|
34
|
+
status = "failure";
|
|
35
|
+
rej(error);
|
|
36
|
+
};
|
|
37
|
+
});
|
|
38
|
+
return {
|
|
39
|
+
resolve,
|
|
40
|
+
status,
|
|
41
|
+
reject,
|
|
42
|
+
promise
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
const NOT_CALLED = Symbol("Subscribe -- Empty Value");
|
|
46
|
+
class Subscribe {
|
|
47
|
+
constructor() {
|
|
48
|
+
this.callbacks = new Map();
|
|
49
|
+
this.deferredResult = deferred();
|
|
50
|
+
this.lastValue = NOT_CALLED;
|
|
51
|
+
this.resetDeferred = null;
|
|
52
|
+
this.publish = this.publish.bind(this);
|
|
53
|
+
this.add = this.add.bind(this);
|
|
54
|
+
this.remove = this.remove.bind(this);
|
|
55
|
+
}
|
|
56
|
+
publish(val) {
|
|
57
|
+
return __async(this, null, function* () {
|
|
58
|
+
var _a;
|
|
59
|
+
yield (_a = this.resetDeferred) == null ? void 0 : _a.promise;
|
|
60
|
+
this.lastValue = val;
|
|
61
|
+
this.deferredResult.resolve(val);
|
|
62
|
+
this.callbacks.forEach((c) => c(val));
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
add(callback, options) {
|
|
66
|
+
const id = this.callbacks.size;
|
|
67
|
+
this.callbacks.set(id, callback);
|
|
68
|
+
if (this.lastValue !== NOT_CALLED && (options == null ? void 0 : options.initializeWithPreviousValue) === true) {
|
|
69
|
+
callback(this.lastValue);
|
|
70
|
+
}
|
|
71
|
+
return id;
|
|
72
|
+
}
|
|
73
|
+
remove(id) {
|
|
74
|
+
this.callbacks.delete(id);
|
|
75
|
+
}
|
|
76
|
+
reset(promise) {
|
|
77
|
+
return __async(this, null, function* () {
|
|
78
|
+
var _a;
|
|
79
|
+
if (promise) {
|
|
80
|
+
this.resetDeferred = deferred();
|
|
81
|
+
yield promise;
|
|
82
|
+
}
|
|
83
|
+
if (this.lastValue !== NOT_CALLED) {
|
|
84
|
+
this.deferredResult = deferred();
|
|
85
|
+
}
|
|
86
|
+
this.lastValue = NOT_CALLED;
|
|
87
|
+
this.callbacks.forEach((c) => c(void 0));
|
|
88
|
+
(_a = this.resetDeferred) == null ? void 0 : _a.resolve();
|
|
89
|
+
this.resetDeferred = null;
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
suspend() {
|
|
93
|
+
if (this.lastValue === NOT_CALLED) {
|
|
94
|
+
throw this.deferredResult.promise;
|
|
95
|
+
}
|
|
96
|
+
return this.lastValue;
|
|
97
|
+
}
|
|
98
|
+
get() {
|
|
99
|
+
if (this.lastValue === NOT_CALLED) {
|
|
100
|
+
return void 0;
|
|
101
|
+
}
|
|
102
|
+
return this.lastValue;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
function useSubscribedState(subscriber) {
|
|
106
|
+
const [state, setState] = React.useState(subscriber.get());
|
|
107
|
+
React.useEffect(() => {
|
|
108
|
+
const id = subscriber.add((resp) => {
|
|
109
|
+
setState(resp);
|
|
110
|
+
}, {
|
|
111
|
+
initializeWithPreviousValue: true
|
|
112
|
+
});
|
|
113
|
+
return () => {
|
|
114
|
+
subscriber.remove(id);
|
|
115
|
+
};
|
|
116
|
+
}, [subscriber]);
|
|
117
|
+
return state;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export { Subscribe, useSubscribedState };
|
|
121
|
+
//# sourceMappingURL=index.esm.js.map
|
package/package.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@player-ui/react-subscribe",
|
|
3
|
+
"version": "0.0.1-next.1",
|
|
4
|
+
"private": false,
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"registry": "https://registry.npmjs.org"
|
|
7
|
+
},
|
|
8
|
+
"peerDependencies": {
|
|
9
|
+
"@types/react": "^17.0.25",
|
|
10
|
+
"react": "^17.0.2"
|
|
11
|
+
},
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"p-defer": "^3.0.0",
|
|
14
|
+
"@babel/runtime": "7.15.4"
|
|
15
|
+
},
|
|
16
|
+
"main": "dist/index.cjs.js",
|
|
17
|
+
"module": "dist/index.esm.js",
|
|
18
|
+
"typings": "dist/index.d.ts"
|
|
19
|
+
}
|
package/src/index.tsx
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
|
|
3
|
+
export type SubscribeID = number;
|
|
4
|
+
|
|
5
|
+
type ResolveType<T> = (arg?: T) => void;
|
|
6
|
+
type RejectType = (error?: Error) => void;
|
|
7
|
+
type StatusType = 'success' | 'failure' | 'pending';
|
|
8
|
+
type DefferedReturnType<T> = {
|
|
9
|
+
/** a function to resolve the promise */
|
|
10
|
+
resolve: ResolveType<T>;
|
|
11
|
+
|
|
12
|
+
/** a function to reject the promise */
|
|
13
|
+
reject: RejectType;
|
|
14
|
+
|
|
15
|
+
/** the status of the promise */
|
|
16
|
+
status: StatusType;
|
|
17
|
+
|
|
18
|
+
/** a promise to express the above */
|
|
19
|
+
promise: Promise<T>;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/** create a deferred promise */
|
|
23
|
+
function deferred<T>(): DefferedReturnType<T> {
|
|
24
|
+
/** the default resolve handler is a noop */
|
|
25
|
+
let resolve: ResolveType<T> = () => undefined;
|
|
26
|
+
|
|
27
|
+
/** the default reject handler is a noop */
|
|
28
|
+
let reject: RejectType = () => undefined;
|
|
29
|
+
|
|
30
|
+
let status: StatusType = 'pending';
|
|
31
|
+
|
|
32
|
+
const promise = new Promise<T>((res, rej) => {
|
|
33
|
+
resolve = (a?: T) => {
|
|
34
|
+
status = 'success';
|
|
35
|
+
const resolveFunc = res as ResolveType<T>;
|
|
36
|
+
resolveFunc(a);
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
reject = (error?: Error) => {
|
|
40
|
+
status = 'failure';
|
|
41
|
+
rej(error);
|
|
42
|
+
};
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
return {
|
|
46
|
+
resolve,
|
|
47
|
+
status,
|
|
48
|
+
reject,
|
|
49
|
+
promise,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const NOT_CALLED = Symbol('Subscribe -- Empty Value');
|
|
54
|
+
/**
|
|
55
|
+
* A pub-sub module that works across the React bridge
|
|
56
|
+
*/
|
|
57
|
+
export class Subscribe<T> {
|
|
58
|
+
private callbacks: Map<SubscribeID, (val: T | undefined) => void> = new Map();
|
|
59
|
+
private deferredResult = deferred<T>();
|
|
60
|
+
private lastValue: T | typeof NOT_CALLED = NOT_CALLED;
|
|
61
|
+
private resetDeferred: DefferedReturnType<void> | null = null;
|
|
62
|
+
constructor() {
|
|
63
|
+
this.publish = this.publish.bind(this);
|
|
64
|
+
this.add = this.add.bind(this);
|
|
65
|
+
this.remove = this.remove.bind(this);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Trigger the subscriptions using the provided value
|
|
70
|
+
* if there is a reset in progress, wait for it before publishing a new value.
|
|
71
|
+
*/
|
|
72
|
+
async publish(val: T): Promise<void> {
|
|
73
|
+
await this.resetDeferred?.promise;
|
|
74
|
+
this.lastValue = val;
|
|
75
|
+
this.deferredResult.resolve(val);
|
|
76
|
+
this.callbacks.forEach((c) => c(val));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Subscribe to updates
|
|
81
|
+
*/
|
|
82
|
+
add(
|
|
83
|
+
callback: (arg: T | undefined) => void,
|
|
84
|
+
options?: {
|
|
85
|
+
/** Use the last updated value for this subscription to immediately trigger the onSet callback */
|
|
86
|
+
initializeWithPreviousValue?: boolean;
|
|
87
|
+
}
|
|
88
|
+
): SubscribeID {
|
|
89
|
+
const id = this.callbacks.size;
|
|
90
|
+
this.callbacks.set(id, callback);
|
|
91
|
+
|
|
92
|
+
if (
|
|
93
|
+
this.lastValue !== NOT_CALLED &&
|
|
94
|
+
options?.initializeWithPreviousValue === true
|
|
95
|
+
) {
|
|
96
|
+
callback(this.lastValue);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return id;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Remove any updates from the given listener
|
|
104
|
+
*/
|
|
105
|
+
remove(id: SubscribeID) {
|
|
106
|
+
this.callbacks.delete(id);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Reset the state of the listener
|
|
111
|
+
* Passing in a promise will defer resetting the view until the promise is resolved
|
|
112
|
+
*/
|
|
113
|
+
async reset(promise?: Promise<void>) {
|
|
114
|
+
if (promise) {
|
|
115
|
+
this.resetDeferred = deferred<void>();
|
|
116
|
+
await promise;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (this.lastValue !== NOT_CALLED) {
|
|
120
|
+
this.deferredResult = deferred();
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
this.lastValue = NOT_CALLED;
|
|
124
|
+
this.callbacks.forEach((c) => c(undefined));
|
|
125
|
+
|
|
126
|
+
this.resetDeferred?.resolve();
|
|
127
|
+
this.resetDeferred = null;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* _Throws_ a promise if the value is still pending
|
|
132
|
+
* Otherwise returns it
|
|
133
|
+
*/
|
|
134
|
+
suspend(): T {
|
|
135
|
+
if (this.lastValue === NOT_CALLED) {
|
|
136
|
+
throw this.deferredResult.promise;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return this.lastValue;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** Get the current value of the subscription */
|
|
143
|
+
get(): T | undefined {
|
|
144
|
+
if (this.lastValue === NOT_CALLED) {
|
|
145
|
+
return undefined;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return this.lastValue;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export interface SubscribedStateHookOptions {
|
|
153
|
+
/** if the state should trigger suspense when waiting to resolve */
|
|
154
|
+
suspend?: boolean;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Subscribe to a state change event in a react component */
|
|
158
|
+
export function useSubscribedState<T>(subscriber: Subscribe<T>): T | undefined {
|
|
159
|
+
const [state, setState] = React.useState<T | undefined>(subscriber.get());
|
|
160
|
+
|
|
161
|
+
React.useEffect(() => {
|
|
162
|
+
const id = subscriber.add(
|
|
163
|
+
(resp) => {
|
|
164
|
+
setState(resp);
|
|
165
|
+
},
|
|
166
|
+
{
|
|
167
|
+
initializeWithPreviousValue: true,
|
|
168
|
+
}
|
|
169
|
+
);
|
|
170
|
+
|
|
171
|
+
return () => {
|
|
172
|
+
subscriber.remove(id);
|
|
173
|
+
};
|
|
174
|
+
}, [subscriber]);
|
|
175
|
+
|
|
176
|
+
return state;
|
|
177
|
+
}
|