@clicktap/state 0.1.0 → 0.1.2

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.
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.timerMachine = void 0;
4
+ const xstate_1 = require("xstate");
5
+ const immer_1 = require("@xstate/immer");
6
+ exports.timerMachine =
7
+ /** @xstate-layout N4IgpgJg5mDOIC5QBcCWBbMAnAdABwEMBXWSAYgCUBRAZQFUBZKgfQBUBJJigbQAYBdRKDwB7WKjQiAdkJAAPRAFoAjADYATDmUBOXQA5tAZmV69AFgDsq1QBoQAT0SHehnAFYAvh7tpMuLERSUqhSUGQcAMIA0nyCSCCi4pIy8QoIJqo4qnoWbnaOCGZmmaq8Zbyqyi5uym6qXj4Y2DgBQSFhAAoAgnQ0LBxcsbKJEqjSsmlVbjimFmZ6agZmbsbq+U4u7g0gvs2twaFkQ-EjyRNObrxZOXkOiGbKFjjq5WbqFurFvNpmXt4gUhEEDgsl2WGGYlG41SSnUVS0um0BmMpks1nW6Tcmk8-zB+GIpAgEKSYxSoDSvAxn142zx+3axKhZPkiEpdwQhjmW1xTVwAGMROg8AAbMDISCMs4whBsgpuOp-DxAA */
8
+ (0, xstate_1.createMachine)({
9
+ id: 'timer',
10
+ initial: 'running',
11
+ context: {
12
+ elapsed: 0,
13
+ duration: 3000,
14
+ interval: 100,
15
+ intervalId: null,
16
+ },
17
+ states: {
18
+ paused: {
19
+ on: {
20
+ // START_TIMER: {
21
+ // target: 'running',
22
+ // cond: (context) => context.elapsed < context.duration,
23
+ // },
24
+ RESUME_TIMER: {
25
+ target: 'running',
26
+ cond: (context) => context.elapsed < context.duration,
27
+ },
28
+ },
29
+ },
30
+ running: {
31
+ invoke: {
32
+ src: (context) => (send) => {
33
+ // eslint-disable-next-line no-console
34
+ // console.log('context.interval: ', context.interval);
35
+ const interval = setInterval(() => {
36
+ send('TICK');
37
+ }, context.interval);
38
+ return () => {
39
+ clearInterval(interval);
40
+ };
41
+ },
42
+ },
43
+ always: [
44
+ {
45
+ target: 'completed',
46
+ cond: (context) => {
47
+ return context.elapsed >= context.duration;
48
+ },
49
+ },
50
+ ],
51
+ on: {
52
+ TICK: {
53
+ actions: (0, immer_1.assign)((context, event) => {
54
+ if (event.type !== 'TICK')
55
+ return;
56
+ context.elapsed += context.interval;
57
+ // eslint-disable-next-line no-console
58
+ // console.log(
59
+ // 'elapsed: %d | interval: %d',
60
+ // context.elapsed,
61
+ // context.interval
62
+ // );
63
+ }),
64
+ },
65
+ PAUSE_TIMER: { target: 'paused' },
66
+ },
67
+ },
68
+ completed: {
69
+ type: 'final',
70
+ },
71
+ },
72
+ });
73
+ exports.default = exports.timerMachine;
74
+ //# sourceMappingURL=timer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"timer.js","sourceRoot":"","sources":["../../../../../libs/state/src/toast/timer.ts"],"names":[],"mappings":";;;AAAA,mCAAuC;AACvC,yCAAuC;AAwC1B,QAAA,YAAY;AACvB,6cAA6c;AAC7c,IAAA,sBAAa,EAA4B;IACvC,EAAE,EAAE,OAAO;IACX,OAAO,EAAE,SAAS;IAClB,OAAO,EAAE;QACP,OAAO,EAAE,CAAC;QACV,QAAQ,EAAE,IAAI;QACd,QAAQ,EAAE,GAAG;QACb,UAAU,EAAE,IAAI;KACjB;IACD,MAAM,EAAE;QACN,MAAM,EAAE;YACN,EAAE,EAAE;gBACF,iBAAiB;gBACjB,uBAAuB;gBACvB,2DAA2D;gBAC3D,KAAK;gBACL,YAAY,EAAE;oBACZ,MAAM,EAAE,SAAS;oBACjB,IAAI,EAAE,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,OAAO,GAAG,OAAO,CAAC,QAAQ;iBACtD;aACF;SACF;QACD,OAAO,EAAE;YACP,MAAM,EAAE;gBACN,GAAG,EAAE,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,IAAI,EAAE,EAAE;oBACzB,sCAAsC;oBACtC,uDAAuD;oBACvD,MAAM,QAAQ,GAAG,WAAW,CAAC,GAAG,EAAE;wBAChC,IAAI,CAAC,MAAM,CAAC,CAAC;oBACf,CAAC,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAC;oBACrB,OAAO,GAAG,EAAE;wBACV,aAAa,CAAC,QAAQ,CAAC,CAAC;oBAC1B,CAAC,CAAC;gBACJ,CAAC;aACF;YACD,MAAM,EAAE;gBACN;oBACE,MAAM,EAAE,WAAW;oBACnB,IAAI,EAAE,CAAC,OAAO,EAAE,EAAE;wBAChB,OAAO,OAAO,CAAC,OAAO,IAAI,OAAO,CAAC,QAAQ,CAAC;oBAC7C,CAAC;iBACF;aACF;YACD,EAAE,EAAE;gBACF,IAAI,EAAE;oBACJ,OAAO,EAAE,IAAA,cAAM,EAAC,CAAC,OAAO,EAAE,KAAK,EAAE,EAAE;wBACjC,IAAI,KAAK,CAAC,IAAI,KAAK,MAAM;4BAAE,OAAO;wBAClC,OAAO,CAAC,OAAO,IAAI,OAAO,CAAC,QAAQ,CAAC;wBACpC,sCAAsC;wBACtC,eAAe;wBACf,kCAAkC;wBAClC,qBAAqB;wBACrB,qBAAqB;wBACrB,KAAK;oBACP,CAAC,CAAC;iBACH;gBACD,WAAW,EAAE,EAAE,MAAM,EAAE,QAAQ,EAAE;aAClC;SACF;QACD,SAAS,EAAE;YACT,IAAI,EAAE,OAAO;SACd;KACF;CACF,CAAC,CAAC;AAEL,kBAAe,oBAAY,CAAC"}
@@ -0,0 +1,46 @@
1
+ import { ReactNode } from 'react';
2
+ import type { ReactElement } from 'react';
3
+ /** @todo figure out TS here */
4
+ export type ToastItem = {
5
+ id: string | null;
6
+ duration: number;
7
+ element: ReactNode;
8
+ };
9
+ export interface ToastMachineContext {
10
+ /** @todo create proper type for Array items */
11
+ items: Array<ToastItem>;
12
+ order: 'asc' | 'desc';
13
+ duration: number;
14
+ activeItem: string | null;
15
+ }
16
+ export interface AddItemEvent {
17
+ type: 'ADD_ITEM';
18
+ item: ReactElement;
19
+ duration?: number;
20
+ }
21
+ export interface RemoveItemEvent {
22
+ type: 'REMOVE_ITEM';
23
+ }
24
+ export interface SetIdleEvent {
25
+ type: 'SET_IDLE';
26
+ }
27
+ export interface SetActiveEvent {
28
+ type: 'SET_ACTIVE';
29
+ }
30
+ export interface ResumeTimerEvent {
31
+ type: 'RESUME_TIMER';
32
+ }
33
+ export interface PauseTimerEvent {
34
+ type: 'PAUSE_TIMER';
35
+ }
36
+ export interface TimeoutEvent {
37
+ type: 'xstate.after(ITEM_TIMEOUT)#toast.active';
38
+ }
39
+ export interface TimerDoneEvent {
40
+ type: 'done.invoke.timer';
41
+ }
42
+ export type ToastMachineEvents = AddItemEvent | RemoveItemEvent | SetIdleEvent | SetActiveEvent | TimeoutEvent | ResumeTimerEvent | PauseTimerEvent | TimerDoneEvent;
43
+ export declare const toastMachine: import("xstate").StateMachine<ToastMachineContext, any, ToastMachineEvents, {
44
+ value: any;
45
+ context: ToastMachineContext;
46
+ }, import("xstate").BaseActionObject, import("xstate").ServiceMap, import("xstate").ResolveTypegenMeta<import("xstate").TypegenDisabled, ToastMachineEvents, import("xstate").BaseActionObject, import("xstate").ServiceMap>>;
@@ -0,0 +1,153 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toastMachine = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const xstate_1 = require("xstate");
6
+ const immer_1 = require("@xstate/immer");
7
+ const react_1 = require("react");
8
+ const crypto_1 = tslib_1.__importDefault(require("crypto"));
9
+ const timer_1 = require("./timer");
10
+ exports.toastMachine =
11
+ /** @xstate-layout N4IgpgJg5mDOIC5QBcD2BDWyB0BLCANmAMQCCAIuQPoCSAKgKICyA2gAwC6ioADqrLmS5UAO24gAHogCMAZgCs2AGzS2SgBxKA7AE4lO2ToAsAGhABPRAFoj6gL52zaTDnQBjIQDcSEUWDwinqgA1v5CALZgAE7sXEggfAJCouJSCEpsOspaOQay0kqyAExKZpYIReps2LY6mmxG+kUKWkYOThhY2O5eJBTU9Myx4omCwmLxabKF2NKN8kpFbPls8gpl1vLqykXyasaNhdJFWu0gzl09uN7EAErMAPIAagy0jKycI-xjKZOIi9sDPI9EZgYV1LItBsEIYlNg9NIdGxKki1jkzhdXB5riQAMoMOi0cgAGQYw3io2SE1AaTkWS0RQOSKWEK0c2hzWkNUWJy0Skah1sGM6WN6dwYuIAqkxXnQaDLbuTeN8qakZPIuXylhktAppgUitDilpsJpQeo9KsVOoCsKXN1sTcAAqkSX4qhyhVKhIq8ZqirSbaZeTyLRbVnqVqyaHSaQm45mtirKo6Eo6O2XCAQXAiKDEfGEmgksmfCm+340xC2bCh3XGrTLfR8+TQqxFJbYTIoow6HStBshjOuLM5vMFqikADCcpe3spfr+MKMmsZbDmFvU6jBRtW8I3IcMBT7Q+wUTA4VQnlH+YJRNJc-L1MkiHURiKps0DIWKhyIZjehqfc1h0I8GyUE8zwvK9cxvQkpxnEs4mVJIF0rBBU1kWZVw1WtgSMUwLHVIxsEqIxjVDIF5CFM4RFQCA4HETEvhQitnwQKxYxNOoDFjbQ2RtfRW1kfJO1THjA1sVRZBPfAiGYn4nzSKx9D3cS+MDI9-zhWot2A0C1BPK5vHk1VF1kIwuOZPICmKYEtMAuoDxAptlkMkdcxM1C2MMYjKhKXUlH5DUimkVsijImsxMMcLpjDUMIPPS9R081i0i3OE10afI42XcKW0ImFgRrYE5FBWx1F2cyHAcIA */
12
+ (0, xstate_1.createMachine)({
13
+ context: { items: [], order: 'desc', duration: 0, activeItem: null },
14
+ predictableActionArguments: true,
15
+ id: 'toast',
16
+ initial: 'idle',
17
+ states: {
18
+ idle: {
19
+ on: {
20
+ ADD_ITEM: {
21
+ target: 'adding',
22
+ },
23
+ },
24
+ },
25
+ active: {
26
+ invoke: {
27
+ id: 'timer',
28
+ src: timer_1.timerMachine,
29
+ onDone: {
30
+ target: 'removing',
31
+ cond: 'itemHasTimeout',
32
+ },
33
+ data: Object.assign(timer_1.timerMachine.context, {
34
+ duration: (context) => {
35
+ var _a;
36
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-return
37
+ return (_a = context.items[context.items.length - 1].duration) !== null && _a !== void 0 ? _a : context.duration;
38
+ },
39
+ }),
40
+ },
41
+ on: {
42
+ ADD_ITEM: {
43
+ // actions: sendTo('timer', 'PAUSE_TIMER'),
44
+ target: 'adding',
45
+ },
46
+ REMOVE_ITEM: {
47
+ target: 'removing',
48
+ },
49
+ SET_IDLE: {
50
+ target: 'idle',
51
+ },
52
+ RESUME_TIMER: {
53
+ actions: (0, xstate_1.sendTo)('timer', 'RESUME_TIMER'),
54
+ // cond: (context) => context.duration > 0,
55
+ },
56
+ PAUSE_TIMER: {
57
+ actions: (0, xstate_1.sendTo)('timer', 'PAUSE_TIMER'),
58
+ // cond: (context) => context.duration > 0,
59
+ },
60
+ },
61
+ // after: {
62
+ // ITEM_TIMEOUT: {
63
+ // target: 'removing',
64
+ // cond: 'itemHasTimeout',
65
+ // },
66
+ // },
67
+ },
68
+ adding: {
69
+ entry: ['addItem'],
70
+ on: {
71
+ SET_IDLE: {
72
+ target: 'idle',
73
+ },
74
+ SET_ACTIVE: {
75
+ target: 'active',
76
+ },
77
+ },
78
+ },
79
+ removing: {
80
+ entry: 'removeItem',
81
+ on: {
82
+ SET_IDLE: {
83
+ target: 'idle',
84
+ },
85
+ SET_ACTIVE: {
86
+ target: 'active',
87
+ },
88
+ },
89
+ },
90
+ },
91
+ }, {
92
+ actions: {
93
+ addItem: (0, immer_1.assign)((context, event) => {
94
+ var _a, _b;
95
+ if (event.type !== 'ADD_ITEM')
96
+ return;
97
+ const id = `notification-${crypto_1.default.randomBytes(16).toString('hex')}`;
98
+ const duration = (_a = event.duration) !== null && _a !== void 0 ? _a : context.duration;
99
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
100
+ const notification = (0, react_1.cloneElement)(event.item, Object.assign({ duration,
101
+ id, key: (_b = event.item.key) !== null && _b !== void 0 ? _b : id }, event.item.props));
102
+ const item = {
103
+ duration,
104
+ id,
105
+ element: notification,
106
+ };
107
+ if (context.order === 'desc') {
108
+ context.items.push(item);
109
+ }
110
+ else {
111
+ context.items.unshift(item);
112
+ }
113
+ context.activeItem = id;
114
+ }),
115
+ removeItem: (0, immer_1.assign)((context, event) => {
116
+ if (event.type !== 'REMOVE_ITEM' &&
117
+ event.type !== 'xstate.after(ITEM_TIMEOUT)#toast.active' &&
118
+ event.type !== 'done.invoke.timer')
119
+ return;
120
+ if (context.order === 'desc') {
121
+ context.items.pop();
122
+ context.activeItem =
123
+ context.items.length > 0
124
+ ? context.items[context.items.length - 1].id
125
+ : null;
126
+ }
127
+ else {
128
+ context.items.shift();
129
+ context.activeItem =
130
+ context.items.length > 0 ? context.items[0].id : null;
131
+ }
132
+ }),
133
+ // startTimer: sendTo('timer', { type: 'START' }),
134
+ },
135
+ delays: {
136
+ ITEM_TIMEOUT: (context) => {
137
+ var _a;
138
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-return
139
+ return (
140
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
141
+ (_a = context.items[context.items.length - 1].duration) !== null && _a !== void 0 ? _a : context.duration);
142
+ },
143
+ },
144
+ guards: {
145
+ itemHasTimeout: (context) => {
146
+ return (
147
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
148
+ context.items[context.items.length - 1].duration > 0 ||
149
+ context.duration > 0);
150
+ },
151
+ },
152
+ });
153
+ //# sourceMappingURL=toast.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"toast.js","sourceRoot":"","sources":["../../../../../libs/state/src/toast/toast.ts"],"names":[],"mappings":";;;;AAAA,mCAA+C;AAC/C,yCAAuC;AACvC,iCAAgD;AAChD,4DAA4B;AAE5B,mCAAuC;AAiE1B,QAAA,YAAY;AACvB,++BAA++B;AAC/+B,IAAA,sBAAa,EACX;IACE,OAAO,EAAE,EAAE,KAAK,EAAE,EAAE,EAAE,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AC,QAAQ,GAAG,CAAC,CACrB,CAAC;QACJ,CAAC;KACF;CACF,CACF,CAAC"}
package/.eslintrc.json DELETED
@@ -1,40 +0,0 @@
1
- {
2
- "extends": [
3
- "../../.eslintrc.json"
4
- ],
5
- "ignorePatterns": [
6
- "!**/*",
7
- "jest.config.ts",
8
- "*.spec.ts"
9
- ],
10
- "overrides": [
11
- {
12
- "files": [
13
- "*.ts",
14
- "*.tsx",
15
- "*.js",
16
- "*.jsx"
17
- ],
18
- "rules": {}
19
- },
20
- {
21
- "files": [
22
- "*.ts",
23
- "*.tsx"
24
- ],
25
- "rules": {},
26
- "parserOptions": {
27
- "project": [
28
- "libs/state/tsconfig.lib.json"
29
- ]
30
- }
31
- },
32
- {
33
- "files": [
34
- "*.js",
35
- "*.jsx"
36
- ],
37
- "rules": {}
38
- }
39
- ]
40
- }
package/jest.config.ts DELETED
@@ -1,10 +0,0 @@
1
- /* eslint-disable */
2
- export default {
3
- displayName: 'state',
4
- preset: '../../jest.preset.js',
5
- transform: {
6
- '^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
7
- },
8
- moduleFileExtensions: ['ts', 'js', 'html'],
9
- coverageDirectory: '../../coverage/libs/state',
10
- };
package/project.json DELETED
@@ -1,40 +0,0 @@
1
- {
2
- "name": "state",
3
- "$schema": "../../node_modules/nx/schemas/project-schema.json",
4
- "sourceRoot": "libs/state/src",
5
- "projectType": "library",
6
- "targets": {
7
- "build": {
8
- "executor": "@nx/js:tsc",
9
- "outputs": ["{options.outputPath}"],
10
- "options": {
11
- "outputPath": "dist/libs/state",
12
- "main": "libs/state/src/index.ts",
13
- "tsConfig": "libs/state/tsconfig.lib.json",
14
- "assets": ["libs/state/*.md"]
15
- }
16
- },
17
- "lint": {
18
- "executor": "@nx/linter:eslint",
19
- "outputs": ["{options.outputFile}"],
20
- "options": {
21
- "lintFilePatterns": ["libs/state/**/*.ts"]
22
- }
23
- },
24
- "test": {
25
- "executor": "@nx/jest:jest",
26
- "outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
27
- "options": {
28
- "jestConfig": "libs/state/jest.config.ts",
29
- "passWithNoTests": true
30
- },
31
- "configurations": {
32
- "ci": {
33
- "ci": true,
34
- "codeCoverage": true
35
- }
36
- }
37
- }
38
- },
39
- "tags": []
40
- }
@@ -1,97 +0,0 @@
1
- import { createContext, useContext } from 'react';
2
- import { interpret } from 'xstate';
3
- import type { InterpreterFrom } from 'xstate';
4
- import cookie from 'cookie';
5
- import type { ReactNode } from 'react';
6
- import type { NextPageContext } from 'next';
7
- import { authMachine } from './auth';
8
-
9
- // removing dependencies on next, this should work regardless of platform
10
- // import type { NextPageContext } from 'next';
11
- // import { useRouter } from 'next/router';
12
-
13
- export const AuthContext = createContext(
14
- {} as InterpreterFrom<typeof authMachine>
15
- );
16
-
17
- export const useAuth = () => {
18
- // console.log('useAuth');
19
- // console.log(useContext(AuthContext).getSnapshot().context);
20
- return useContext(AuthContext);
21
- };
22
-
23
- export const useUser = () => {
24
- return useContext(AuthContext).getSnapshot().context.user;
25
- };
26
-
27
- type AuthOptions = {
28
- devTools: boolean;
29
- endpoints: {
30
- login: string;
31
- logout: string;
32
- refresh: string;
33
- ssrRefresh: string;
34
- };
35
- };
36
-
37
- /**
38
- * this will run on the server side as part of app.getInitialProps
39
- * @todo is it possible to share state from client to server? headers? cookies?
40
- */
41
- export const getAuth = async (
42
- context: NextPageContext,
43
- options: AuthOptions
44
- // eslint-disable-next-line @typescript-eslint/require-await
45
- ) => {
46
- if (typeof window !== 'undefined') {
47
- // eslint-disable-next-line no-console
48
- console.warn(
49
- 'App.getInitialProps::getAuth should not be run on the frontend. You are probably missing getServerSideProps in your page.'
50
- );
51
- return interpret(authMachine, { devTools: options.devTools }).start();
52
- }
53
-
54
- const cookies = cookie.parse(
55
- context?.req?.headers && context.req.headers.cookie
56
- ? context.req.headers.cookie
57
- : ''
58
- );
59
- if (typeof cookies['refresh_token'] !== 'undefined') {
60
- const authContext = {
61
- ...authMachine.initialState.context,
62
- refreshToken: cookies['refresh_token'],
63
- endpoints: { ...options.endpoints },
64
- };
65
- return interpret(authMachine.withContext(authContext), {
66
- devTools: options.devTools,
67
- }).start();
68
- }
69
-
70
- return interpret(authMachine, { devTools: options.devTools }).start();
71
- };
72
-
73
- type Props = {
74
- children: ReactNode;
75
- service: InterpreterFrom<typeof authMachine>;
76
- // options: {
77
- // devTools: boolean;
78
- // };
79
- // state: StateConfig<AuthMachineContext, AuthMachineEvents>;
80
- };
81
-
82
- export function AuthProvider({
83
- children,
84
- service,
85
- }: // options = { devTools: false },
86
- // state
87
- Props) {
88
- // console.log('AuthProvider::state');
89
- // console.log(state.context);
90
- // let authService = interpret(authMachine, { devTools: options.devTools }).start(state);
91
- // console.log('AuthProvider::authService');
92
- // console.log(authService.getSnapshot().context);
93
-
94
- return (
95
- <AuthContext.Provider value={service}>{children}</AuthContext.Provider>
96
- );
97
- }
@@ -1,7 +0,0 @@
1
- import { authMachine } from './auth';
2
-
3
- describe('state', () => {
4
- it('should work', () => {
5
- expect(authMachine).toEqual('auth');
6
- });
7
- });