react-native-wgpu 0.1.11 → 0.1.12

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.
Files changed (58) hide show
  1. package/cpp/webgpu/webgpu.h +4182 -5
  2. package/cpp/webgpu/webgpu_cpp.h +9009 -5
  3. package/lib/commonjs/hooks.js +61 -1
  4. package/lib/commonjs/hooks.js.map +1 -1
  5. package/lib/commonjs/index.js +0 -8
  6. package/lib/commonjs/index.js.map +1 -1
  7. package/lib/module/hooks.js +57 -1
  8. package/lib/module/hooks.js.map +1 -1
  9. package/lib/module/index.js +0 -5
  10. package/lib/module/index.js.map +1 -1
  11. package/lib/typescript/lib/commonjs/hooks.d.ts +11 -0
  12. package/lib/typescript/lib/commonjs/hooks.d.ts.map +1 -1
  13. package/lib/typescript/lib/commonjs/index.d.ts +0 -1
  14. package/lib/typescript/lib/commonjs/index.d.ts.map +1 -1
  15. package/lib/typescript/lib/module/hooks.d.ts +11 -0
  16. package/lib/typescript/lib/module/hooks.d.ts.map +1 -1
  17. package/lib/typescript/lib/module/index.d.ts +0 -1
  18. package/lib/typescript/lib/module/index.d.ts.map +1 -1
  19. package/lib/typescript/src/hooks.d.ts +18 -0
  20. package/lib/typescript/src/hooks.d.ts.map +1 -1
  21. package/lib/typescript/src/index.d.ts +0 -1
  22. package/lib/typescript/src/index.d.ts.map +1 -1
  23. package/libs/apple/arm64_iphoneos/libwebgpu_dawn.a +0 -0
  24. package/libs/apple/arm64_iphonesimulator/libwebgpu_dawn.a +0 -0
  25. package/libs/apple/arm64_xros/libwebgpu_dawn.a +0 -0
  26. package/libs/apple/arm64_xrsimulator/libwebgpu_dawn.a +0 -0
  27. package/libs/apple/iphonesimulator/libwebgpu_dawn.a +0 -0
  28. package/libs/apple/libwebgpu_dawn.xcframework/Info.plist +8 -8
  29. package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64/libwebgpu_dawn.a +0 -0
  30. package/libs/apple/libwebgpu_dawn.xcframework/ios-arm64_x86_64-simulator/libwebgpu_dawn.a +0 -0
  31. package/libs/apple/libwebgpu_dawn.xcframework/macos-arm64_x86_64/libwebgpu_dawn.a +0 -0
  32. package/libs/apple/libwebgpu_dawn.xcframework/xros-arm64/libwebgpu_dawn.a +0 -0
  33. package/libs/apple/libwebgpu_dawn.xcframework/xros-arm64-simulator/libwebgpu_dawn.a +0 -0
  34. package/libs/apple/universal_macosx/libwebgpu_dawn.a +0 -0
  35. package/libs/apple/x86_64_iphonesimulator/libwebgpu_dawn.a +0 -0
  36. package/package.json +2 -2
  37. package/react-native-wgpu.podspec +1 -1
  38. package/src/hooks.tsx +125 -0
  39. package/src/index.tsx +0 -8
  40. package/cpp/dawn/dawn_proc_table.h +0 -308
  41. package/cpp/dawn/webgpu.h +0 -4210
  42. package/cpp/dawn/webgpu_cpp.h +0 -9037
  43. package/cpp/dawn/webgpu_cpp_print.h +0 -2466
  44. package/cpp/dawn/wire/client/webgpu.h +0 -339
  45. package/cpp/dawn/wire/client/webgpu_cpp.h +0 -9177
  46. package/ios/IOSPlatformContext.h +0 -19
  47. package/ios/IOSPlatformContext.mm +0 -68
  48. package/ios/MetalView.h +0 -10
  49. package/ios/MetalView.mm +0 -40
  50. package/ios/SurfaceUtils.h +0 -15
  51. package/ios/SurfaceUtils.mm +0 -34
  52. package/ios/WebGPUModule.h +0 -19
  53. package/ios/WebGPUModule.mm +0 -93
  54. package/ios/WebGPUView.h +0 -18
  55. package/ios/WebGPUView.mm +0 -86
  56. package/ios/WebGPUViewComponentDescriptor.h +0 -61
  57. package/ios/WebGPUViewManager.mm +0 -23
  58. package/src/hooks.ts +0 -53
@@ -1,19 +0,0 @@
1
- #pragma once
2
-
3
- #include "PlatformContext.h"
4
-
5
- namespace rnwgpu {
6
-
7
- class IOSPlatformContext : public PlatformContext {
8
- public:
9
- IOSPlatformContext() = default;
10
- ~IOSPlatformContext() = default;
11
-
12
- wgpu::Surface makeSurface(wgpu::Instance instance, void *surface, int width,
13
- int height) override;
14
-
15
- ImageData createImageBitmap(std::string blobId, double offset,
16
- double size) override;
17
- };
18
-
19
- } // namespace rnwgpu
@@ -1,68 +0,0 @@
1
- #include "IOSPlatformContext.h"
2
-
3
- #import <React/RCTBlobManager.h>
4
- #import <React/RCTBridge+Private.h>
5
- #import <ReactCommon/RCTTurboModule.h>
6
-
7
- #include "RNWebGPUManager.h"
8
- #include "WebGPUModule.h"
9
-
10
- namespace rnwgpu {
11
-
12
- wgpu::Surface IOSPlatformContext::makeSurface(wgpu::Instance instance,
13
- void *surface, int width,
14
- int height) {
15
- wgpu::SurfaceDescriptorFromMetalLayer metalSurfaceDesc;
16
- metalSurfaceDesc.layer = surface;
17
- wgpu::SurfaceDescriptor surfaceDescriptor;
18
- surfaceDescriptor.nextInChain = &metalSurfaceDesc;
19
- return instance.CreateSurface(&surfaceDescriptor);
20
- }
21
-
22
- ImageData IOSPlatformContext::createImageBitmap(std::string blobId,
23
- double offset, double size) {
24
- RCTBlobManager *blobManager =
25
- [[RCTBridge currentBridge] moduleForClass:RCTBlobManager.class];
26
- NSData *blobData =
27
- [blobManager resolve:[NSString stringWithUTF8String:blobId.c_str()]
28
- offset:(long)offset
29
- size:(long)size];
30
-
31
- if (!blobData) {
32
- throw std::runtime_error("Couldn't retrive blob data");
33
- }
34
-
35
- UIImage *image = [UIImage imageWithData:blobData];
36
- if (!image) {
37
- throw std::runtime_error("Couldn't decode image");
38
- }
39
-
40
- CGImageRef cgImage = image.CGImage;
41
- size_t width = CGImageGetWidth(cgImage);
42
- size_t height = CGImageGetHeight(cgImage);
43
- size_t bitsPerComponent = 8;
44
- size_t bytesPerRow = width * 4;
45
- std::vector<uint8_t> imageData(height * bytesPerRow);
46
-
47
- CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
48
- CGContextRef context = CGBitmapContextCreate(
49
- imageData.data(), width, height, bitsPerComponent, bytesPerRow,
50
- colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
51
-
52
- CGContextDrawImage(context, CGRectMake(0, 0, width, height), cgImage);
53
-
54
- // Now imageData contains a copy of the bitmap data
55
-
56
- CGContextRelease(context);
57
- CGColorSpaceRelease(colorSpace);
58
-
59
- // Use the copied data
60
- ImageData result;
61
- result.width = static_cast<int>(width);
62
- result.height = static_cast<int>(height);
63
- result.data = imageData;
64
- result.format = wgpu::TextureFormat::RGBA8Unorm;
65
- return result;
66
- }
67
-
68
- } // namespace rnwgpu
package/ios/MetalView.h DELETED
@@ -1,10 +0,0 @@
1
- #pragma once
2
-
3
- #import "WebGPUModule.h"
4
- #import <UIKit/UIKit.h>
5
-
6
- @interface MetalView : UIView
7
-
8
- @property NSNumber *contextId;
9
-
10
- @end
package/ios/MetalView.mm DELETED
@@ -1,40 +0,0 @@
1
- #import "MetalView.h"
2
- #import "SurfaceUtils.h"
3
- #import "webgpu_cpp.h"
4
- #import <React/RCTViewManager.h>
5
-
6
- @implementation MetalView {
7
- BOOL _isConfigured;
8
- }
9
-
10
- + (Class)layerClass {
11
- return [CAMetalLayer class];
12
- }
13
-
14
- // Paper only method
15
- // TODO: this method is wrong, it appears to call configureSurface instead of
16
- // updateSurface sometimes
17
- - (void)reactSetFrame:(CGRect)frame {
18
- [super reactSetFrame:frame];
19
- if (!_isConfigured) {
20
- [SurfaceUtils configureSurface:self.layer
21
- size:self.frame.size
22
- contextId:[_contextId intValue]];
23
- _isConfigured = YES;
24
- } else {
25
- [SurfaceUtils updateSurface:[_contextId intValue] size:self.frame.size];
26
- }
27
- }
28
-
29
- - (void)willMoveToSuperview:(UIView *)newSuperview {
30
- [super willMoveToSuperview:newSuperview];
31
-
32
- if (newSuperview == nil) {
33
- // The view is being removed from its superview
34
- // Add your cleanup code here
35
- [SurfaceUtils cleanupSurface:[_contextId intValue]];
36
- _isConfigured = NO;
37
- }
38
- }
39
-
40
- @end
@@ -1,15 +0,0 @@
1
- #pragma once
2
-
3
- #import <UIKit/UIKit.h>
4
-
5
- @interface SurfaceUtils : NSObject
6
-
7
- + (void)configureSurface:(CALayer *)layer
8
- size:(CGSize)size
9
- contextId:(int)contextId;
10
-
11
- + (void)updateSurface:(int)contextId size:(CGSize)size;
12
-
13
- + (void)cleanupSurface:(int)contextId;
14
-
15
- @end
@@ -1,34 +0,0 @@
1
- #import "SurfaceUtils.h"
2
- #import "MetalView.h"
3
- #import "WebGPUModule.h"
4
- #import "WebGPUView.h"
5
-
6
- @implementation SurfaceUtils
7
-
8
- + (void)configureSurface:(CALayer *)layer
9
- size:(CGSize)size
10
- contextId:(int)contextId {
11
- std::shared_ptr<rnwgpu::RNWebGPUManager> manager = [WebGPUModule getManager];
12
- void *nativeSurface = (__bridge void *)layer;
13
- auto &registry = rnwgpu::SurfaceRegistry::getInstance();
14
- auto gpu = manager->_gpu;
15
- auto surface = manager->_platformContext->makeSurface(
16
- gpu, nativeSurface, size.width, size.height);
17
- registry.getSurfaceInfoOrCreate(contextId, gpu, size.width, size.height)
18
- ->switchToOnscreen(nativeSurface, surface);
19
- }
20
-
21
- + (void)updateSurface:(int)contextId size:(CGSize)size {
22
- // std::shared_ptr<rnwgpu::RNWebGPUManager> manager = [WebGPUModule
23
- // getManager];
24
- auto &registry = rnwgpu::SurfaceRegistry::getInstance();
25
- registry.getSurfaceInfo(contextId)->resize(size.width, size.height);
26
- }
27
-
28
- + (void)cleanupSurface:(int)contextId {
29
- auto &registry = rnwgpu::SurfaceRegistry::getInstance();
30
- // Remove the surface info from the registry
31
- registry.removeSurfaceInfo(contextId);
32
- }
33
-
34
- @end
@@ -1,19 +0,0 @@
1
- #pragma once
2
-
3
- #import "RNWebGPUManager.h"
4
- #import <React/RCTBridgeModule.h>
5
- #import <React/RCTEventEmitter.h>
6
-
7
- #ifdef RCT_NEW_ARCH_ENABLED
8
- #import <RNWgpuViewSpec/RNWgpuViewSpec.h>
9
- @interface WebGPUModule : RCTEventEmitter <NativeWebGPUModuleSpec>
10
- #else
11
- @interface WebGPUModule : RCTEventEmitter <RCTBridgeModule>
12
- #endif
13
-
14
- @property(nonatomic, weak) RCTBridge *bridge;
15
- @property(nonatomic, weak) RCTModuleRegistry *moduleRegistry;
16
-
17
- + (std::shared_ptr<rnwgpu::RNWebGPUManager>)getManager;
18
-
19
- @end
@@ -1,93 +0,0 @@
1
- #import "WebGPUModule.h"
2
- #import "GPUCanvasContext.h"
3
- #include "IOSPlatformContext.h"
4
-
5
- #import <React/RCTBridge+Private.h>
6
- #import <React/RCTLog.h>
7
- #import <React/RCTUIManagerUtils.h>
8
- #import <ReactCommon/RCTTurboModule.h>
9
- #import <jsi/jsi.h>
10
- #import <memory>
11
-
12
- namespace jsi = facebook::jsi;
13
- namespace react = facebook::react;
14
-
15
- @implementation WebGPUModule
16
-
17
- RCT_EXPORT_MODULE(WebGPUModule)
18
-
19
- static std::shared_ptr<rnwgpu::RNWebGPUManager> webgpuManager;
20
-
21
- + (std::shared_ptr<rnwgpu::RNWebGPUManager>)getManager {
22
- return webgpuManager;
23
- }
24
-
25
- #pragma Setup and invalidation
26
-
27
- + (BOOL)requiresMainQueueSetup {
28
- return YES;
29
- }
30
-
31
- - (void)invalidate {
32
- // if (_webgpuManager != nil) {
33
- // [_webgpuManager invalidate];
34
- // }
35
- webgpuManager = nil;
36
- }
37
-
38
- - (std::shared_ptr<rnwgpu::RNWebGPUManager>)getManager {
39
- return webgpuManager;
40
- }
41
-
42
- RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(install) {
43
- if (webgpuManager != nil) {
44
- // Already initialized, ignore call.
45
- return @true;
46
- }
47
- RCTCxxBridge *cxxBridge = (RCTCxxBridge *)[RCTBridge currentBridge];
48
- if (!cxxBridge.runtime) {
49
- NSLog(@"Failed to install react-native-filament: RCTBridge is not a "
50
- @"RCTCxxBridge!");
51
- return [NSNumber numberWithBool:NO];
52
- }
53
-
54
- jsi::Runtime *runtime = (jsi::Runtime *)cxxBridge.runtime;
55
- if (!runtime) {
56
- NSLog(@"Failed to install react-native-filament: jsi::Runtime* was null!");
57
- return [NSNumber numberWithBool:NO];
58
- }
59
- std::shared_ptr<react::CallInvoker> jsInvoker = cxxBridge.jsCallInvoker;
60
- if (!jsInvoker) {
61
- NSLog(@"Failed to install react-native-filament: react::CallInvoker was "
62
- @"null!");
63
- return [NSNumber numberWithBool:NO];
64
- }
65
-
66
- if (!jsInvoker) {
67
- jsInvoker = cxxBridge.jsCallInvoker;
68
- }
69
- std::shared_ptr<rnwgpu::PlatformContext> platformContext =
70
- std::make_shared<rnwgpu::IOSPlatformContext>();
71
- // TODO: remove allocation here
72
- webgpuManager = std::make_shared<rnwgpu::RNWebGPUManager>(runtime, jsInvoker,
73
- platformContext);
74
- return @true;
75
- }
76
-
77
- // RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(createSurfaceContext
78
- // : (double)contextId) {
79
- // int contextIdInt = contextId;
80
- // RCTCxxBridge *cxxBridge = (RCTCxxBridge *)self.bridge;
81
- // auto runtime = (jsi::Runtime *)cxxBridge.runtime;
82
-
83
- // return @true;
84
- // }
85
-
86
- #ifdef RCT_NEW_ARCH_ENABLED
87
- - (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
88
- (const facebook::react::ObjCTurboModule::InitParams &)params {
89
- return std::make_shared<facebook::react::NativeWebGPUModuleSpecJSI>(params);
90
- }
91
- #endif
92
-
93
- @end
package/ios/WebGPUView.h DELETED
@@ -1,18 +0,0 @@
1
- #ifdef RCT_NEW_ARCH_ENABLED
2
- #pragma once
3
-
4
- #import "MetalView.h"
5
- #import <React/RCTViewComponentView.h>
6
- #import <UIKit/UIKit.h>
7
-
8
- NS_ASSUME_NONNULL_BEGIN
9
-
10
- @interface WebGPUView : RCTViewComponentView
11
- + (void)registerMetalView:(MetalView *)metalView
12
- withContextId:(NSNumber *)contextId;
13
- + (bool)isContextRegisterd:(NSNumber *)contextId;
14
- @end
15
-
16
- NS_ASSUME_NONNULL_END
17
-
18
- #endif /* RCT_NEW_ARCH_ENABLED */
package/ios/WebGPUView.mm DELETED
@@ -1,86 +0,0 @@
1
- #ifdef RCT_NEW_ARCH_ENABLED
2
- #import "WebGPUView.h"
3
-
4
- #import <react/renderer/components/RNWgpuViewSpec/EventEmitters.h>
5
- #import <react/renderer/components/RNWgpuViewSpec/Props.h>
6
- #import <react/renderer/components/RNWgpuViewSpec/RCTComponentViewHelpers.h>
7
-
8
- #import "MetalView.h"
9
- #import "RCTFabricComponentsPlugins.h"
10
- #import "Utils.h"
11
- #import "WebGPUModule.h"
12
- #import "WebGPUViewComponentDescriptor.h"
13
-
14
- using namespace facebook::react;
15
-
16
- @interface WebGPUView () <RCTWebGPUViewViewProtocol>
17
-
18
- @end
19
-
20
- @implementation WebGPUView {
21
- NSNumber *_contextId;
22
- }
23
-
24
- static NSMutableDictionary<NSNumber *, MetalView *> *metalViewRegistry =
25
- [NSMutableDictionary new];
26
-
27
- + (ComponentDescriptorProvider)componentDescriptorProvider {
28
- return concreteComponentDescriptorProvider<WebGPUViewComponentDescriptor>();
29
- }
30
-
31
- + (void)registerMetalView:(MetalView *)metalView
32
- withContextId:(NSNumber *)contextId {
33
- metalViewRegistry[contextId] = metalView;
34
- }
35
-
36
- + (bool)isContextRegisterd:(NSNumber *)contextId {
37
- return metalViewRegistry[contextId] != nil;
38
- }
39
-
40
- - (instancetype)initWithFrame:(CGRect)frame {
41
- if (self = [super initWithFrame:frame]) {
42
- static const auto defaultProps = std::make_shared<const WebGPUViewProps>();
43
- _props = defaultProps;
44
- }
45
-
46
- return self;
47
- }
48
-
49
- - (void)prepareForRecycle {
50
- [super prepareForRecycle];
51
- self.contentView = nil;
52
- if ([metalViewRegistry objectForKey:_contextId] != nil) {
53
- [metalViewRegistry removeObjectForKey:_contextId];
54
- }
55
- }
56
-
57
- - (void)updateProps:(const facebook::react::Props::Shared &)props
58
- oldProps:(const facebook::react::Props::Shared &)oldProps {
59
- const auto &oldViewProps =
60
- *std::static_pointer_cast<const WebGPUViewProps>(_props);
61
- const auto &newViewProps =
62
- *std::static_pointer_cast<const WebGPUViewProps>(props);
63
-
64
- if (newViewProps.contextId != oldViewProps.contextId) {
65
- _contextId = [[NSNumber alloc] initWithInt:newViewProps.contextId];
66
- }
67
-
68
- [super updateProps:props oldProps:oldProps];
69
- }
70
-
71
- - (void)updateLayoutMetrics:
72
- (const facebook::react::LayoutMetrics &)layoutMetrics
73
- oldLayoutMetrics:
74
- (const facebook::react::LayoutMetrics &)oldLayoutMetrics {
75
- [super updateLayoutMetrics:layoutMetrics oldLayoutMetrics:oldLayoutMetrics];
76
- if (!self.contentView) {
77
- const auto &props =
78
- *std::static_pointer_cast<WebGPUViewProps const>(_props);
79
- self.contentView = metalViewRegistry[@(props.contextId)];
80
- }
81
- }
82
-
83
- Class<RCTComponentViewProtocol> WebGPUViewCls(void) { return WebGPUView.class; }
84
-
85
- @end
86
- #endif
@@ -1,61 +0,0 @@
1
- #ifdef RCT_NEW_ARCH_ENABLED
2
-
3
- #pragma once
4
-
5
- #include <react/debug/react_native_assert.h>
6
- #include <react/renderer/components/RNWgpuViewSpec/ShadowNodes.h>
7
- #include <react/renderer/components/view/ConcreteViewShadowNode.h>
8
- #include <react/renderer/core/ConcreteComponentDescriptor.h>
9
- #include <react/renderer/core/LayoutContext.h>
10
-
11
- #import "MetalView.h"
12
- #import "SurfaceUtils.h"
13
- #import "WebGPUView.h"
14
-
15
- namespace facebook {
16
- namespace react {
17
-
18
- class WebGPUViewCustomShadowNode final : public WebGPUViewShadowNode {
19
-
20
- public:
21
- using ConcreteViewShadowNode::ConcreteViewShadowNode;
22
-
23
- void layout(LayoutContext layoutContext) override {
24
- YogaLayoutableShadowNode::layout(layoutContext);
25
- configureSurface();
26
- }
27
-
28
- void configureSurface() {
29
- const auto &viewProps =
30
- *std::static_pointer_cast<WebGPUViewProps const>(props_);
31
- if ([WebGPUView isContextRegisterd:@(viewProps.contextId)]) {
32
- return;
33
- }
34
- __block MetalView *metalView;
35
- __block CALayer *layer;
36
- dispatch_sync(dispatch_get_main_queue(), ^{
37
- metalView = [[MetalView alloc] init];
38
- layer = metalView.layer;
39
- });
40
- [WebGPUView registerMetalView:metalView
41
- withContextId:@(viewProps.contextId)];
42
-
43
- // TODO: use physical size
44
- float width = layoutMetrics_.frame.size.width;
45
- float height = layoutMetrics_.frame.size.height;
46
- [SurfaceUtils configureSurface:layer
47
- size:CGSizeMake(width, height)
48
- contextId:viewProps.contextId];
49
- }
50
- };
51
-
52
- class WebGPUViewComponentDescriptor final
53
- : public ConcreteComponentDescriptor<WebGPUViewCustomShadowNode> {
54
- public:
55
- using ConcreteComponentDescriptor::ConcreteComponentDescriptor;
56
- };
57
-
58
- } // namespace react
59
- } // namespace facebook
60
-
61
- #endif // RCT_NEW_ARCH_ENABLED
@@ -1,23 +0,0 @@
1
- #import "MetalView.h"
2
- #import "RCTBridge.h"
3
- #import "WebGPUModule.h"
4
- #import <React/RCTUIManager.h>
5
- #import <React/RCTViewManager.h>
6
-
7
- @interface WebGPUViewManager : RCTViewManager
8
- @end
9
-
10
- @implementation WebGPUViewManager
11
-
12
- RCT_EXPORT_MODULE(WebGPUView)
13
-
14
- - (UIView *)view {
15
- return [MetalView new];
16
- }
17
-
18
- RCT_CUSTOM_VIEW_PROPERTY(contextId, NSNumber, UIView) {
19
- NSNumber *contextId = [RCTConvert NSNumber:json];
20
- [(MetalView *)view setContextId:contextId];
21
- }
22
-
23
- @end
package/src/hooks.ts DELETED
@@ -1,53 +0,0 @@
1
- import { useEffect, useRef, useState } from "react";
2
-
3
- import type { RNCanvasContext, CanvasRef, NativeCanvas } from "./Canvas";
4
-
5
- type Unsubscribe = () => void;
6
-
7
- export const useSurface = () => {
8
- const [surface, setSurface] = useState<NativeCanvas | null>(null);
9
- const ref = useCanvasEffect(() => {
10
- const sur = ref.current!.getNativeSurface();
11
- setSurface(sur);
12
- });
13
- return { ref, surface };
14
- };
15
-
16
- export const useGPUContext = () => {
17
- const [context, setContext] = useState<RNCanvasContext | null>(null);
18
- const ref = useCanvasEffect(() => {
19
- const ctx = ref.current!.getContext("webgpu")!;
20
- setContext(ctx);
21
- });
22
- return { ref, context };
23
- };
24
-
25
- export const useCanvasEffect = (
26
- effect: () =>
27
- | void
28
- | Unsubscribe
29
- | Promise<Unsubscribe | void>
30
- | Promise<void>,
31
- ) => {
32
- const unsub = useRef<Unsubscribe | null | Promise<Unsubscribe | void>>(null);
33
- const ref = useRef<CanvasRef>(null);
34
- useEffect(() => {
35
- ref.current!.whenReady(async () => {
36
- const sub = effect();
37
- if (sub) {
38
- unsub.current = sub;
39
- }
40
- });
41
- return () => {
42
- if (unsub.current) {
43
- if (unsub.current instanceof Promise) {
44
- unsub.current.then((sub) => sub && sub());
45
- } else {
46
- unsub.current();
47
- }
48
- }
49
- };
50
- // eslint-disable-next-line react-hooks/exhaustive-deps
51
- }, []);
52
- return ref;
53
- };