rexr 0.2.0 → 0.4.0

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.
@@ -6,38 +6,42 @@ interface BatteryState {
6
6
  chargingTime: number;
7
7
  dischargingTime: number;
8
8
  callback(): void;
9
- onChange(): void;
10
- onMount(): void;
11
- onClean(): void;
9
+ change(): void;
10
+ mount(): void;
11
+ clean(): void;
12
12
  }
13
+ declare type BatteryArg = Partial<BatteryState> | BatteryState['change'];
13
14
 
14
15
  interface ClipboardState {
15
16
  snapshot: [ClipboardState];
16
17
  clipboard: string;
17
18
  callback(): void;
18
- onChange(): void;
19
- onMount(): void;
20
- onClean(): void;
19
+ change(): void;
20
+ mount(): void;
21
+ clean(): void;
21
22
  }
23
+ declare type ClipboardArg = Partial<ClipboardState> | ClipboardState['change'];
22
24
 
23
25
  interface GeolocationState {
24
26
  snapshot: [GeolocationState];
25
27
  geolocation: any;
26
28
  id: number;
27
29
  callback(): void;
28
- onChange(): void;
29
- onMount(): void;
30
- onClean(): void;
30
+ change(): void;
31
+ mount(): void;
32
+ clean(): void;
31
33
  }
34
+ declare type GeolocationArg = Partial<GeolocationState> | GeolocationState['change'];
32
35
 
33
36
  interface OnlineState {
34
37
  snapshot: [OnlineState];
35
38
  online: boolean;
36
39
  callback(): void;
37
- onChange(): void;
38
- onMount(): void;
39
- onClean(): void;
40
+ change(): void;
41
+ mount(): void;
42
+ clean(): void;
40
43
  }
44
+ declare type OnlineArg = Partial<OnlineState> | OnlineState['change'];
41
45
 
42
46
  declare type Vec3 = [x: number, y: number, z: number];
43
47
 
@@ -52,19 +56,21 @@ interface OrientState {
52
56
  movement: Vec3;
53
57
  memo: any;
54
58
  callback(): void;
55
- onChange(): void;
56
- onMount(): void;
57
- onClean(): void;
59
+ change(): void;
60
+ mount(): void;
61
+ clean(): void;
58
62
  }
63
+ declare type OrientArg = Partial<OrientState> | OrientState['change'];
59
64
 
60
65
  interface WindowSizeState {
61
66
  snapshot: [WindowSizeState];
62
67
  width: number;
63
68
  height: number;
64
69
  callback(): void;
65
- onChange(): void;
66
- onMount(): void;
67
- onClean(): void;
70
+ change(): void;
71
+ mount(): void;
72
+ clean(): void;
68
73
  }
74
+ declare type WindowSizeArg = Partial<WindowSizeState> | WindowSizeState['change'];
69
75
 
70
- export { BatteryState as B, ClipboardState as C, GeolocationState as G, OnlineState as O, WindowSizeState as W, OrientState as a };
76
+ export { BatteryState as B, ClipboardState as C, GeolocationState as G, OnlineState as O, WindowSizeState as W, OrientState as a, BatteryArg as b, ClipboardArg as c, GeolocationArg as d, OnlineArg as e, OrientArg as f, WindowSizeArg as g };
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "rexr",
3
3
  "author": "tseijp",
4
4
  "license": "MIT",
5
- "version": "0.2.0",
5
+ "version": "0.4.0",
6
6
  "private": false,
7
7
  "sideEffect": false,
8
8
  "publishConfig": {
@@ -29,11 +29,35 @@
29
29
  ],
30
30
  "files": [
31
31
  "dist/**/*.{js,ts,mjs,mts,map}",
32
+ "src/**/*.{js,ts,mjs,mts,map}",
32
33
  "packages.json",
33
34
  "README.md"
34
35
  ],
35
36
  "exports": {
36
37
  "./packages.json": "./packages.json",
38
+ "./src": {
39
+ "types": "./src/index.ts",
40
+ "import": "./src/index.ts",
41
+ "default": "./src/index.ts"
42
+ },
43
+ "./src/react": {
44
+ "types": "./src/react.ts",
45
+ "import": "./src/react.ts",
46
+ "default": "./src/react.ts"
47
+ },
48
+ "./react": {
49
+ "types": "./dist/react.d.ts",
50
+ "import": {
51
+ "types": "./dist/react.d.ts",
52
+ "default": "./dist/react.mjs"
53
+ },
54
+ "module": {
55
+ "types": "./dist/react.d.ts",
56
+ "default": "./dist/react.mjs"
57
+ },
58
+ "require": "./dist/react.js",
59
+ "default": "./dist/react.js"
60
+ },
37
61
  ".": {
38
62
  "types": "./dist/index.d.ts",
39
63
  "import": {
@@ -59,35 +83,37 @@
59
83
  },
60
84
  "require": "./dist/index.js",
61
85
  "default": "./dist/index.js"
86
+ }
87
+ },
88
+ "dependencies": {
89
+ "reev": "0.12.0"
90
+ },
91
+ "peerDependencies": {
92
+ "expo": "*",
93
+ "expo-gl": "*",
94
+ "react": ">=16.8",
95
+ "react-dom": ">=16.8",
96
+ "solid-js": "*",
97
+ "vue": "*"
98
+ },
99
+ "peerDependenciesMeta": {
100
+ "expo": {
101
+ "optional": true
62
102
  },
63
- "./react": {
64
- "types": "./dist/react.d.ts",
65
- "import": {
66
- "types": "./dist/react.d.ts",
67
- "default": "./dist/react.mjs"
68
- },
69
- "module": {
70
- "types": "./dist/react.d.ts",
71
- "default": "./dist/react.mjs"
72
- },
73
- "require": "./dist/react.js",
74
- "default": "./dist/react.js"
103
+ "expo-gl": {
104
+ "optional": true
75
105
  },
76
- "./react/*": {
77
- "types": "./dist/react.d.ts",
78
- "import": {
79
- "types": "./dist/react.d.ts",
80
- "default": "./dist/react.mjs"
81
- },
82
- "module": {
83
- "types": "./dist/react.d.ts",
84
- "default": "./dist/react.mjs"
85
- },
86
- "require": "./dist/react.js",
87
- "default": "./dist/react.js"
106
+ "react": {
107
+ "optional": true
108
+ },
109
+ "react-dom": {
110
+ "optional": true
111
+ },
112
+ "solid-js": {
113
+ "optional": true
114
+ },
115
+ "vue": {
116
+ "optional": true
88
117
  }
89
- },
90
- "devDependencies": {
91
- "reev": "*"
92
118
  }
93
119
  }
@@ -0,0 +1,44 @@
1
+ import { EventState, event } from 'reev/src'
2
+ import { BatteryState } from './types'
3
+
4
+ export * from './types'
5
+
6
+ export const batteryEvent = () => {
7
+ const change = async () => {
8
+ // @ts-ignore
9
+ const battery = await navigator.getBattery()
10
+ self.level = battery.level
11
+ self.charging = battery.charging
12
+ self.chargingTime = battery.chargingTime
13
+ self.dischargingTime = battery.dischargingTime
14
+ self.snapshot = [self]
15
+ self.callback()
16
+ }
17
+
18
+ const mount = () => {
19
+ // @ts-ignore
20
+ navigator.getBattery().then((target: any) => {
21
+ self.target = target
22
+ target.addEventListener('levelchange', self.change)
23
+ target.addEventListener('chargingchange', self.change)
24
+ })
25
+ self.change()
26
+ }
27
+
28
+ const clean = () => {
29
+ const target = self.target
30
+ if (!target) return
31
+ target.removeEventListener('levelchange', self.change)
32
+ target.removeEventListener('chargingchange', self.change)
33
+ }
34
+
35
+ const self = event({
36
+ change,
37
+ mount,
38
+ clean,
39
+ }) as EventState<BatteryState>
40
+
41
+ self.snapshot = [self]
42
+
43
+ return self
44
+ }
@@ -0,0 +1,19 @@
1
+ import { batteryEvent } from '.'
2
+ import { useSyncExternalStore } from 'react'
3
+ import { BatteryState } from './types'
4
+ import { EventState } from 'reev/src'
5
+
6
+ let self: EventState<BatteryState>
7
+
8
+ const subscribe = (callback = () => {}) => {
9
+ self({ callback }).mount()
10
+ return () => self({ callback }).clean()
11
+ }
12
+
13
+ const getSnapshot = () => self.snapshot
14
+
15
+ export const useBattery = () => {
16
+ if (!self) self = batteryEvent()
17
+ const [ret] = useSyncExternalStore(subscribe, getSnapshot, getSnapshot)
18
+ return ret
19
+ }
@@ -0,0 +1,14 @@
1
+ export interface BatteryState {
2
+ snapshot: [BatteryState]
3
+ target: any
4
+ level: number // 0 ~ 1
5
+ charging: boolean
6
+ chargingTime: number // 3180
7
+ dischargingTime: number // Infinity
8
+ callback(): void
9
+ change(): void
10
+ mount(): void
11
+ clean(): void
12
+ }
13
+
14
+ export type BatteryArg = Partial<BatteryState> | BatteryState['change']
@@ -0,0 +1,37 @@
1
+ import { EventState, event } from 'reev/src'
2
+ import { ClipboardState } from './types'
3
+
4
+ export * from './types'
5
+
6
+ export const clipboardEvent = () => {
7
+ const change = () => {
8
+ navigator.clipboard.readText().then((clipboard) => {
9
+ self.clipboard = clipboard
10
+ self.snapshot = [self]
11
+ self.callback()
12
+ })
13
+ }
14
+
15
+ const mount = () => {
16
+ window.addEventListener('copy', self.change)
17
+ window.addEventListener('cut', self.change)
18
+ window.addEventListener('paste', self.change)
19
+ }
20
+
21
+ const clean = () => {
22
+ window.removeEventListener('copy', self.change)
23
+ window.removeEventListener('cut', self.change)
24
+ window.removeEventListener('paste', self.change)
25
+ }
26
+
27
+ const self = event({
28
+ clipboard: '',
29
+ change,
30
+ mount,
31
+ clean,
32
+ }) as EventState<ClipboardState>
33
+
34
+ self.snapshot = [self]
35
+
36
+ return self
37
+ }
@@ -0,0 +1,19 @@
1
+ import { useSyncExternalStore } from 'react'
2
+ import { clipboardEvent } from '.'
3
+ import { EventState } from 'reev/src'
4
+ import { ClipboardState } from './types'
5
+
6
+ let self: EventState<ClipboardState>
7
+
8
+ const subscribe = (callback = () => {}) => {
9
+ self({ callback }).mount()
10
+ return () => self({ callback }).clean()
11
+ }
12
+
13
+ const getSnapshot = () => self.snapshot
14
+
15
+ export const useClipboard = () => {
16
+ if (!self) self = clipboardEvent()
17
+ const [ret] = useSyncExternalStore(subscribe, getSnapshot, getSnapshot)
18
+ return ret
19
+ }
@@ -0,0 +1,10 @@
1
+ export interface ClipboardState {
2
+ snapshot: [ClipboardState]
3
+ clipboard: string
4
+ callback(): void
5
+ change(): void
6
+ mount(): void
7
+ clean(): void
8
+ }
9
+
10
+ export type ClipboardArg = Partial<ClipboardState> | ClipboardState['change']
@@ -0,0 +1,33 @@
1
+ import { EventState, event } from 'reev/src'
2
+ import { GeolocationState } from './types'
3
+
4
+ export * from './types'
5
+
6
+ export const geolocationEvent = () => {
7
+ const change = () => {
8
+ navigator.geolocation.getCurrentPosition((geolocation) => {
9
+ self.geolocation = geolocation
10
+ self.snapshot = [self]
11
+ self.callback()
12
+ })
13
+ }
14
+
15
+ const mount = () => {
16
+ self.change()
17
+ self.id = navigator.geolocation.watchPosition(self.change)
18
+ }
19
+
20
+ const clean = () => {
21
+ if (typeof self.id !== 'undefined') navigator.geolocation.clearWatch(self.id)
22
+ }
23
+
24
+ const self = event({
25
+ change,
26
+ mount,
27
+ clean,
28
+ }) as EventState<GeolocationState>
29
+
30
+ self.snapshot = [self]
31
+
32
+ return self
33
+ }
@@ -0,0 +1,19 @@
1
+ import { EventState } from 'reev/src'
2
+ import { useSyncExternalStore } from 'react'
3
+ import { GeolocationState } from './types'
4
+ import { geolocationEvent } from '.'
5
+
6
+ let self: EventState<GeolocationState>
7
+
8
+ const subscribe = (callback = () => {}) => {
9
+ self({ callback }).mount()
10
+ return () => self({ callback }).clean()
11
+ }
12
+
13
+ const getSnapshot = () => self.snapshot
14
+
15
+ export const useGeolocation = () => {
16
+ if (!self) self = geolocationEvent()
17
+ const [ret] = useSyncExternalStore(subscribe, getSnapshot, getSnapshot)
18
+ return ret
19
+ }
@@ -0,0 +1,11 @@
1
+ export interface GeolocationState {
2
+ snapshot: [GeolocationState]
3
+ geolocation: any
4
+ id: number
5
+ callback(): void
6
+ change(): void
7
+ mount(): void
8
+ clean(): void
9
+ }
10
+
11
+ export type GeolocationArg = Partial<GeolocationState> | GeolocationState['change']
package/src/index.ts ADDED
@@ -0,0 +1,7 @@
1
+ export * from './battery'
2
+ export * from './clipboard'
3
+ export * from './geolocation'
4
+ export * from './mediaQuery'
5
+ export * from './online'
6
+ export * from './orient'
7
+ export * from './windowSize'
@@ -0,0 +1,5 @@
1
+ import { event } from 'reev/src'
2
+
3
+ export * from './types'
4
+
5
+ export const mediaQueryEvent = () => {}
@@ -0,0 +1 @@
1
+ export const useMediaQuery = () => {}
@@ -0,0 +1,9 @@
1
+ export interface MediaQueryState {
2
+ snapshot: [MediaQueryState]
3
+ callback(): void
4
+ change(): void
5
+ mount(): void
6
+ clean(): void
7
+ }
8
+
9
+ export type MediaQueryArg = Partial<MediaQueryState> | MediaQueryState['change']
@@ -0,0 +1,31 @@
1
+ import { OnlineState } from './types'
2
+ import { EventState, event } from 'reev/src'
3
+
4
+ export * from './types'
5
+
6
+ export const onlineEvent = () => {
7
+ const change = () => {
8
+ self.online = navigator.onLine
9
+ self.snapshot = [self]
10
+ self.callback()
11
+ }
12
+
13
+ const mount = () => {
14
+ self.change()
15
+ window.addEventListener('online', self.change)
16
+ window.addEventListener('offline', self.change)
17
+ }
18
+ const clean = () => {
19
+ window.removeEventListener('online', self.change)
20
+ window.removeEventListener('offline', self.change)
21
+ }
22
+ const self = event({
23
+ change,
24
+ mount,
25
+ clean,
26
+ }) as EventState<OnlineState>
27
+
28
+ self.snapshot = [self]
29
+
30
+ return self
31
+ }
@@ -0,0 +1,19 @@
1
+ import { useSyncExternalStore } from 'react'
2
+ import { onlineEvent } from '.'
3
+ import { EventState } from 'reev/src'
4
+ import { OnlineState } from './types'
5
+
6
+ let self: EventState<OnlineState>
7
+
8
+ const subscribe = (callback = () => {}) => {
9
+ self({ callback }).mount()
10
+ return () => self({ callback }).clean()
11
+ }
12
+
13
+ const getSnapshot = () => self.snapshot
14
+
15
+ export const useOnline = () => {
16
+ if (!self) self = onlineEvent()
17
+ const [ret] = useSyncExternalStore(subscribe, getSnapshot, getSnapshot)
18
+ return ret
19
+ }
@@ -0,0 +1,10 @@
1
+ export interface OnlineState {
2
+ snapshot: [OnlineState]
3
+ online: boolean
4
+ callback(): void
5
+ change(): void
6
+ mount(): void
7
+ clean(): void
8
+ }
9
+
10
+ export type OnlineArg = Partial<OnlineState> | OnlineState['change']
@@ -0,0 +1,56 @@
1
+ import { EventState, event } from 'reev/src'
2
+ import { OrientState } from './types'
3
+ import { addVec3, subVec3, vec3 } from './utils'
4
+
5
+ export * from './types'
6
+
7
+ export const orientEvent = () => {
8
+ const initValues = () => {
9
+ self.active = self._active = false
10
+ vec3(0, 0, 0, self.value)
11
+ vec3(0, 0, 0, self._value)
12
+ vec3(0, 0, 0, self.delta)
13
+ vec3(0, 0, 0, self.movement)
14
+ }
15
+
16
+ const change = (e: any) => {
17
+ self._active = self.active
18
+ self._value = self.value
19
+ self.value = [90 - (e.alpha ?? 90), e.beta ?? 0, e.gamma ?? 0]
20
+ if (self._active) {
21
+ subVec3(self.value, self._value, self.delta)
22
+ addVec3(self.offset, self.delta, self.offset)
23
+ addVec3(self.offset, self.delta, self.movement)
24
+ }
25
+ self.snapshot = [self]
26
+ self.callback()
27
+ }
28
+
29
+ const mount = () => {
30
+ initValues()
31
+ self.active = true
32
+ window.addEventListener('deviceorientation', self.change)
33
+ }
34
+
35
+ const clean = () => {
36
+ initValues()
37
+ window.removeEventListener('deviceorientation', self.change)
38
+ }
39
+
40
+ const self = event({
41
+ active: false,
42
+ _active: false,
43
+ _value: vec3(),
44
+ value: vec3(),
45
+ delta: vec3(),
46
+ offset: vec3(),
47
+ movement: vec3(),
48
+ mount,
49
+ clean,
50
+ change,
51
+ }) as EventState<OrientState>
52
+
53
+ self.snapshot = [self]
54
+
55
+ return self
56
+ }
@@ -0,0 +1,19 @@
1
+ import { useSyncExternalStore } from 'react'
2
+ import { orientEvent } from '.'
3
+ import { OrientState } from './types'
4
+ import { EventState } from 'reev/src'
5
+
6
+ let self: EventState<OrientState>
7
+
8
+ const subscribe = (callback = () => {}) => {
9
+ self({ callback }).mount()
10
+ return () => self({ callback }).clean()
11
+ }
12
+
13
+ const getSnapshot = () => self.snapshot
14
+
15
+ export const useOrient = () => {
16
+ if (!self) self = orientEvent()
17
+ const [ret] = useSyncExternalStore(subscribe, getSnapshot, getSnapshot)
18
+ return ret
19
+ }
@@ -0,0 +1,19 @@
1
+ import { Vec3 } from './utils'
2
+
3
+ export interface OrientState {
4
+ snapshot: [OrientState]
5
+ active: boolean
6
+ _active: boolean
7
+ _value: Vec3
8
+ value: Vec3
9
+ delta: Vec3
10
+ offset: Vec3
11
+ movement: Vec3
12
+ memo: any
13
+ callback(): void
14
+ change(): void
15
+ mount(): void
16
+ clean(): void
17
+ }
18
+
19
+ export type OrientArg = Partial<OrientState> | OrientState['change']
@@ -0,0 +1,36 @@
1
+ /**
2
+ * CALCULATE VECTOR
3
+ * REF: https://github.com/toji/gl-matrix/blob/master/src/vec2.js
4
+ */
5
+
6
+ const Vec = typeof Float32Array !== 'undefined' ? Float32Array : Array
7
+
8
+ export const vec3 = (x = 0, y = 0, z = 0, out = new Vec(3)): Vec3 => {
9
+ out[0] = x
10
+ out[1] = y
11
+ out[2] = z
12
+ return out as Vec3
13
+ }
14
+
15
+ export type Vec3 = [x: number, y: number, z: number]
16
+
17
+ export const addVec3 = (a: Vec3, b: Vec3, out = vec3()): Vec3 => {
18
+ out[0] = a[0] + b[0]
19
+ out[1] = a[1] + b[1]
20
+ out[2] = a[2] + b[2]
21
+ return out
22
+ }
23
+
24
+ export const subVec3 = (a: Vec3, b: Vec3, out = vec3()): Vec3 => {
25
+ out[0] = a[0] - b[0]
26
+ out[1] = a[1] - b[1]
27
+ out[2] = a[2] - b[2]
28
+ return out
29
+ }
30
+
31
+ export const cpVec3 = (a: Vec3, out = vec3()): Vec3 => {
32
+ out[0] = a[0]
33
+ out[1] = a[1]
34
+ out[2] = a[2]
35
+ return out
36
+ }
package/src/react.ts ADDED
@@ -0,0 +1,7 @@
1
+ export * from './battery/react'
2
+ export * from './clipboard/react'
3
+ export * from './geolocation/react'
4
+ export * from './mediaQuery/react'
5
+ export * from './online/react'
6
+ export * from './orient/react'
7
+ export * from './windowSize/react'
package/src/utils.ts ADDED
@@ -0,0 +1 @@
1
+ export const isF = (f: unknown): f is Function => typeof f === 'function'