Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
R
react-native-threads
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Jira
Jira
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
冷佳娟
react-native-threads
Commits
30000648
Commit
30000648
authored
Sep 16, 2017
by
Travis Nuttall
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
rename to Thread
parent
5987c1e3
Changes
11
Show whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
122 additions
and
128 deletions
+122
-128
example/ThreadExample/index.ios.js
example/ThreadExample/index.ios.js
+2
-2
index.js
index.js
+2
-7
ios/RNThread.xcodeproj/project.pbxproj
ios/RNThread.xcodeproj/project.pbxproj
+8
-8
ios/ThreadManager.h
ios/ThreadManager.h
+4
-4
ios/ThreadManager.m
ios/ThreadManager.m
+88
-0
ios/ThreadSelfManager.h
ios/ThreadSelfManager.h
+4
-4
ios/WorkerManager.m
ios/WorkerManager.m
+0
-88
ios/WorkerSelfManager.m
ios/WorkerSelfManager.m
+5
-5
js/Thread.js
js/Thread.js
+6
-6
js/self.js
js/self.js
+3
-3
package.json
package.json
+0
-1
No files found.
example/ThreadExample/index.ios.js
View file @
30000648
...
@@ -7,11 +7,11 @@ import {
...
@@ -7,11 +7,11 @@ import {
TouchableOpacity
,
TouchableOpacity
,
}
from
'
react-native
'
;
}
from
'
react-native
'
;
import
{
worker
}
from
'
react-native-thread
'
;
import
{
Thread
}
from
'
react-native-thread
'
;
class
ThreadExample
extends
Component
{
class
ThreadExample
extends
Component
{
componentDidMount
()
{
componentDidMount
()
{
this
.
worker
=
new
worker
(
'
worker.js
'
);
this
.
worker
=
new
Thread
(
'
worker.js
'
);
this
.
worker
.
onmessage
=
(
message
)
=>
{
this
.
worker
.
onmessage
=
(
message
)
=>
{
console
.
log
(
"
Got message from worker
"
,
message
);
console
.
log
(
"
Got message from worker
"
,
message
);
...
...
index.js
View file @
30000648
export
self
from
'
./js/self
'
;
export
{
default
as
self
}
from
'
./js/self
'
;
export
worker
from
'
./js/worker
'
;
export
{
default
as
Thread
}
from
'
./js/Thread
'
;
// import { NativeModules } from 'react-native';
//
// const { RNThread } = NativeModules;
//
// export default RNThread;
ios/RNThread.xcodeproj/project.pbxproj
View file @
30000648
...
@@ -7,7 +7,7 @@
...
@@ -7,7 +7,7 @@
objects
=
{
objects
=
{
/* Begin PBXBuildFile section */
/* Begin PBXBuildFile section */
C039B44A1F6CF2A4009CB65E
/*
WorkerManager.m in Sources */
=
{
isa
=
PBXBuildFile
;
fileRef
=
C039B4471F6CF2A4009CB65E
/* Worker
Manager.m */
;
};
C039B44A1F6CF2A4009CB65E
/*
ThreadManager.m in Sources */
=
{
isa
=
PBXBuildFile
;
fileRef
=
C039B4471F6CF2A4009CB65E
/* Thread
Manager.m */
;
};
C039B44B1F6CF2A4009CB65E
/* WorkerSelfManager.m in Sources */
=
{
isa
=
PBXBuildFile
;
fileRef
=
C039B4491F6CF2A4009CB65E
/* WorkerSelfManager.m */
;
};
C039B44B1F6CF2A4009CB65E
/* WorkerSelfManager.m in Sources */
=
{
isa
=
PBXBuildFile
;
fileRef
=
C039B4491F6CF2A4009CB65E
/* WorkerSelfManager.m */
;
};
/* End PBXBuildFile section */
/* End PBXBuildFile section */
...
@@ -25,9 +25,9 @@
...
@@ -25,9 +25,9 @@
/* Begin PBXFileReference section */
/* Begin PBXFileReference section */
134814201AA4EA6300B7C361
/* libRNThread.a */
=
{
isa
=
PBXFileReference
;
explicitFileType
=
archive.ar
;
includeInIndex
=
0
;
path
=
libRNThread.a
;
sourceTree
=
BUILT_PRODUCTS_DIR
;
};
134814201AA4EA6300B7C361
/* libRNThread.a */
=
{
isa
=
PBXFileReference
;
explicitFileType
=
archive.ar
;
includeInIndex
=
0
;
path
=
libRNThread.a
;
sourceTree
=
BUILT_PRODUCTS_DIR
;
};
C039B4461F6CF2A4009CB65E
/*
WorkerManager.h */
=
{
isa
=
PBXFileReference
;
fileEncoding
=
4
;
lastKnownFileType
=
sourcecode.c.h
;
path
=
Worker
Manager.h
;
sourceTree
=
"<group>"
;
};
C039B4461F6CF2A4009CB65E
/*
ThreadManager.h */
=
{
isa
=
PBXFileReference
;
fileEncoding
=
4
;
lastKnownFileType
=
sourcecode.c.h
;
path
=
Thread
Manager.h
;
sourceTree
=
"<group>"
;
};
C039B4471F6CF2A4009CB65E
/*
WorkerManager.m */
=
{
isa
=
PBXFileReference
;
fileEncoding
=
4
;
lastKnownFileType
=
sourcecode.c.objc
;
path
=
Worker
Manager.m
;
sourceTree
=
"<group>"
;
};
C039B4471F6CF2A4009CB65E
/*
ThreadManager.m */
=
{
isa
=
PBXFileReference
;
fileEncoding
=
4
;
lastKnownFileType
=
sourcecode.c.objc
;
path
=
Thread
Manager.m
;
sourceTree
=
"<group>"
;
};
C039B4481F6CF2A4009CB65E
/*
WorkerSelfManager.h */
=
{
isa
=
PBXFileReference
;
fileEncoding
=
4
;
lastKnownFileType
=
sourcecode.c.h
;
path
=
Worker
SelfManager.h
;
sourceTree
=
"<group>"
;
};
C039B4481F6CF2A4009CB65E
/*
ThreadSelfManager.h */
=
{
isa
=
PBXFileReference
;
fileEncoding
=
4
;
lastKnownFileType
=
sourcecode.c.h
;
path
=
Thread
SelfManager.h
;
sourceTree
=
"<group>"
;
};
C039B4491F6CF2A4009CB65E
/* WorkerSelfManager.m */
=
{
isa
=
PBXFileReference
;
fileEncoding
=
4
;
lastKnownFileType
=
sourcecode.c.objc
;
path
=
WorkerSelfManager.m
;
sourceTree
=
"<group>"
;
};
C039B4491F6CF2A4009CB65E
/* WorkerSelfManager.m */
=
{
isa
=
PBXFileReference
;
fileEncoding
=
4
;
lastKnownFileType
=
sourcecode.c.objc
;
path
=
WorkerSelfManager.m
;
sourceTree
=
"<group>"
;
};
/* End PBXFileReference section */
/* End PBXFileReference section */
...
@@ -53,9 +53,9 @@
...
@@ -53,9 +53,9 @@
58B511D21A9E6C8500147676
=
{
58B511D21A9E6C8500147676
=
{
isa
=
PBXGroup
;
isa
=
PBXGroup
;
children
=
(
children
=
(
C039B4461F6CF2A4009CB65E
/*
Worker
Manager.h */
,
C039B4461F6CF2A4009CB65E
/*
Thread
Manager.h */
,
C039B4471F6CF2A4009CB65E
/*
Worker
Manager.m */
,
C039B4471F6CF2A4009CB65E
/*
Thread
Manager.m */
,
C039B4481F6CF2A4009CB65E
/*
Worker
SelfManager.h */
,
C039B4481F6CF2A4009CB65E
/*
Thread
SelfManager.h */
,
C039B4491F6CF2A4009CB65E
/* WorkerSelfManager.m */
,
C039B4491F6CF2A4009CB65E
/* WorkerSelfManager.m */
,
134814211AA4EA7D00B7C361
/* Products */
,
134814211AA4EA7D00B7C361
/* Products */
,
);
);
...
@@ -117,7 +117,7 @@
...
@@ -117,7 +117,7 @@
isa
=
PBXSourcesBuildPhase
;
isa
=
PBXSourcesBuildPhase
;
buildActionMask
=
2147483647
;
buildActionMask
=
2147483647
;
files
=
(
files
=
(
C039B44A1F6CF2A4009CB65E
/*
Worker
Manager.m in Sources */
,
C039B44A1F6CF2A4009CB65E
/*
Thread
Manager.m in Sources */
,
C039B44B1F6CF2A4009CB65E
/* WorkerSelfManager.m in Sources */
,
C039B44B1F6CF2A4009CB65E
/* WorkerSelfManager.m in Sources */
,
);
);
runOnlyForDeploymentPostprocessing
=
0
;
runOnlyForDeploymentPostprocessing
=
0
;
...
...
ios/
Worker
Manager.h
→
ios/
Thread
Manager.h
View file @
30000648
#ifndef
Worker
Manager_h
#ifndef
Thread
Manager_h
#define
Worker
Manager_h
#define
Thread
Manager_h
#import "
Worker
SelfManager.h"
#import "
Thread
SelfManager.h"
#import <React/RCTBridge.h>
#import <React/RCTBridge.h>
#import <React/RCTBridge+Private.h>
#import <React/RCTBridge+Private.h>
#import <React/RCTEventDispatcher.h>
#import <React/RCTEventDispatcher.h>
#import <React/RCTBundleURLProvider.h>
#import <React/RCTBundleURLProvider.h>
@interface
Worker
Manager
:
NSObject
<
RCTBridgeModule
>
@interface
Thread
Manager
:
NSObject
<
RCTBridgeModule
>
@end
@end
#endif
#endif
ios/ThreadManager.m
0 → 100644
View file @
30000648
#import "ThreadManager.h"
#include <stdlib.h>
@implementation
ThreadManager
@synthesize
bridge
=
_bridge
;
NSMutableDictionary
*
threads
;
RCT_EXPORT_MODULE
();
RCT_REMAP_METHOD
(
startThread
,
name
:
(
NSString
*
)
name
resolver
:
(
RCTPromiseResolveBlock
)
resolve
rejecter
:
(
RCTPromiseRejectBlock
)
reject
)
{
if
(
threads
==
nil
)
{
threads
=
[[
NSMutableDictionary
alloc
]
init
];
}
int
threadId
=
abs
(
arc4random
());
NSURL
*
threadURL
=
[[
RCTBundleURLProvider
sharedSettings
]
jsBundleURLForBundleRoot
:
name
fallbackResource
:
nil
];
NSLog
(
@"starting Thread %@"
,
[
threadURL
absoluteString
]);
RCTBridge
*
threadBridge
=
[[
RCTBridge
alloc
]
initWithBundleURL
:
threadURL
moduleProvider:
nil
launchOptions:
nil
];
ThreadSelfManager
*
threadSelf
=
[
threadBridge
moduleForName
:
@"ThreadSelfManager"
];
[
threadSelf
setThreadId
:
threadId
];
[
threadSelf
setParentBridge
:
self
.
bridge
];
[
threads
setObject
:
threadBridge
forKey
:[
NSNumber
numberWithInt
:
threadId
]];
resolve
([
NSNumber
numberWithInt
:
threadId
]);
}
RCT_EXPORT_METHOD
(
stopThread
:
(
int
)
threadId
)
{
if
(
threads
==
nil
)
{
NSLog
(
@"Empty list of threads. abort stopping thread with id %i"
,
threadId
);
return
;
}
RCTBridge
*
threadBridge
=
threads
[[
NSNumber
numberWithInt
:
threadId
]];
if
(
threadBridge
==
nil
)
{
NSLog
(
@"Thread is NIl. abort stopping thread with id %i"
,
threadId
);
return
;
}
[
threadBridge
invalidate
];
[
threads
removeObjectForKey
:[
NSNumber
numberWithInt
:
threadId
]];
}
RCT_EXPORT_METHOD
(
postThreadMessage
:
(
int
)
threadId
message
:
(
NSString
*
)
message
)
{
if
(
threads
==
nil
)
{
NSLog
(
@"Empty list of threads. abort posting to thread with id %i"
,
threadId
);
return
;
}
RCTBridge
*
threadBridge
=
threads
[[
NSNumber
numberWithInt
:
threadId
]];
if
(
threadBridge
==
nil
)
{
NSLog
(
@"Thread is NIl. abort posting to thread with id %i"
,
threadId
);
return
;
}
[
threadBridge
.
eventDispatcher
sendAppEventWithName
:
@"ThreadMessage"
body:
message
];
}
-
(
void
)
invalidate
{
if
(
threads
==
nil
)
{
return
;
}
for
(
NSNumber
*
threadId
in
threads
)
{
RCTBridge
*
threadBridge
=
threads
[
threadId
];
[
threadBridge
invalidate
];
}
[
threads
removeAllObjects
];
threads
=
nil
;
}
@end
ios/
Worker
SelfManager.h
→
ios/
Thread
SelfManager.h
View file @
30000648
#ifndef
Worker
SelfManager_h
#ifndef
Thread
SelfManager_h
#define
Worker
SelfManager_h
#define
Thread
SelfManager_h
#import <React/RCTBridge.h>
#import <React/RCTBridge.h>
#import <React/RCTBridge+Private.h>
#import <React/RCTBridge+Private.h>
#import <React/RCTEventDispatcher.h>
#import <React/RCTEventDispatcher.h>
@interface
Worker
SelfManager
:
NSObject
<
RCTBridgeModule
>
@interface
Thread
SelfManager
:
NSObject
<
RCTBridgeModule
>
@property
int
worker
Id
;
@property
int
thread
Id
;
@property
RCTBridge
*
parentBridge
;
@property
RCTBridge
*
parentBridge
;
@end
@end
...
...
ios/WorkerManager.m
deleted
100644 → 0
View file @
5987c1e3
#import "WorkerManager.h"
#include <stdlib.h>
@implementation
WorkerManager
@synthesize
bridge
=
_bridge
;
NSMutableDictionary
*
workers
;
RCT_EXPORT_MODULE
();
RCT_REMAP_METHOD
(
startWorker
,
name
:
(
NSString
*
)
name
resolver
:
(
RCTPromiseResolveBlock
)
resolve
rejecter
:
(
RCTPromiseRejectBlock
)
reject
)
{
if
(
workers
==
nil
)
{
workers
=
[[
NSMutableDictionary
alloc
]
init
];
}
int
workerId
=
abs
(
arc4random
());
NSURL
*
workerURL
=
[[
RCTBundleURLProvider
sharedSettings
]
jsBundleURLForBundleRoot
:
name
fallbackResource
:
nil
];
NSLog
(
@"starting Worker %@"
,
[
workerURL
absoluteString
]);
RCTBridge
*
workerBridge
=
[[
RCTBridge
alloc
]
initWithBundleURL
:
workerURL
moduleProvider:
nil
launchOptions:
nil
];
WorkerSelfManager
*
workerSelf
=
[
workerBridge
moduleForName
:
@"WorkerSelfManager"
];
[
workerSelf
setWorkerId
:
workerId
];
[
workerSelf
setParentBridge
:
self
.
bridge
];
[
workers
setObject
:
workerBridge
forKey
:[
NSNumber
numberWithInt
:
workerId
]];
resolve
([
NSNumber
numberWithInt
:
workerId
]);
}
RCT_EXPORT_METHOD
(
stopWorker
:
(
int
)
workerId
)
{
if
(
workers
==
nil
)
{
NSLog
(
@"Empty list of workers. abort stopping worker with id %i"
,
workerId
);
return
;
}
RCTBridge
*
workerBridge
=
workers
[[
NSNumber
numberWithInt
:
workerId
]];
if
(
workerBridge
==
nil
)
{
NSLog
(
@"Worker is NIl. abort stopping worker with id %i"
,
workerId
);
return
;
}
[
workerBridge
invalidate
];
[
workers
removeObjectForKey
:[
NSNumber
numberWithInt
:
workerId
]];
}
RCT_EXPORT_METHOD
(
postWorkerMessage
:
(
int
)
workerId
message
:
(
NSString
*
)
message
)
{
if
(
workers
==
nil
)
{
NSLog
(
@"Empty list of workers. abort posting to worker with id %i"
,
workerId
);
return
;
}
RCTBridge
*
workerBridge
=
workers
[[
NSNumber
numberWithInt
:
workerId
]];
if
(
workerBridge
==
nil
)
{
NSLog
(
@"Worker is NIl. abort posting to worker with id %i"
,
workerId
);
return
;
}
[
workerBridge
.
eventDispatcher
sendAppEventWithName
:
@"WorkerMessage"
body:
message
];
}
-
(
void
)
invalidate
{
if
(
workers
==
nil
)
{
return
;
}
for
(
NSNumber
*
workerId
in
workers
)
{
RCTBridge
*
workerBridge
=
workers
[
workerId
];
[
workerBridge
invalidate
];
}
[
workers
removeAllObjects
];
workers
=
nil
;
}
@end
ios/WorkerSelfManager.m
View file @
30000648
#import "
Worker
SelfManager.h"
#import "
Thread
SelfManager.h"
#include <stdlib.h>
#include <stdlib.h>
@implementation
Worker
SelfManager
@implementation
Thread
SelfManager
RCT_EXPORT_MODULE
();
RCT_EXPORT_MODULE
();
@synthesize
bridge
=
_bridge
;
@synthesize
bridge
=
_bridge
;
@synthesize
parentBridge
=
_parentBridge
;
@synthesize
parentBridge
=
_parentBridge
;
@synthesize
workerId
=
_worker
Id
;
@synthesize
threadId
=
_thread
Id
;
RCT_EXPORT_METHOD
(
postMessage
:
(
NSString
*
)
message
)
RCT_EXPORT_METHOD
(
postMessage
:
(
NSString
*
)
message
)
{
{
if
(
self
.
parentBridge
==
nil
)
{
if
(
self
.
parentBridge
==
nil
)
{
NSLog
(
@"No parent bridge defined - abord sending
worker
message"
);
NSLog
(
@"No parent bridge defined - abord sending
thread
message"
);
return
;
return
;
}
}
NSString
*
eventName
=
[
NSString
stringWithFormat
:
@"
Worker%i"
,
self
.
worker
Id
];
NSString
*
eventName
=
[
NSString
stringWithFormat
:
@"
thread%i"
,
self
.
thread
Id
];
[
self
.
parentBridge
.
eventDispatcher
sendAppEventWithName
:
eventName
[
self
.
parentBridge
.
eventDispatcher
sendAppEventWithName
:
eventName
body:
message
];
body:
message
];
...
...
js/
worker
.js
→
js/
Thread
.js
View file @
30000648
...
@@ -3,17 +3,17 @@ import {
...
@@ -3,17 +3,17 @@ import {
DeviceEventEmitter
,
DeviceEventEmitter
,
}
from
'
react-native
'
;
}
from
'
react-native
'
;
const
{
Worker
Manager
}
=
NativeModules
;
const
{
Thread
Manager
}
=
NativeModules
;
export
default
class
Worker
{
export
default
class
Thread
{
constructor
(
jsPath
)
{
constructor
(
jsPath
)
{
if
(
!
jsPath
||
!
jsPath
.
endsWith
(
'
.js
'
))
{
if
(
!
jsPath
||
!
jsPath
.
endsWith
(
'
.js
'
))
{
throw
new
Error
(
"
Invalid worker path. Only js files are supported
"
);
throw
new
Error
(
"
Invalid worker path. Only js files are supported
"
);
}
}
this
.
id
=
WorkerManager
.
startWorker
(
jsPath
.
replace
(
"
.js
"
,
""
))
this
.
id
=
ThreadManager
.
startThread
(
jsPath
.
replace
(
"
.js
"
,
""
))
.
then
(
id
=>
{
.
then
(
id
=>
{
DeviceEventEmitter
.
addListener
(
`
Worker
${
id
}
`
,
(
message
)
=>
{
DeviceEventEmitter
.
addListener
(
`
Thread
${
id
}
`
,
(
message
)
=>
{
!!
message
&&
this
.
onmessage
&&
this
.
onmessage
(
message
);
!!
message
&&
this
.
onmessage
&&
this
.
onmessage
(
message
);
});
});
return
id
;
return
id
;
...
@@ -22,10 +22,10 @@ export default class Worker {
...
@@ -22,10 +22,10 @@ export default class Worker {
}
}
postMessage
(
message
)
{
postMessage
(
message
)
{
this
.
id
.
then
(
id
=>
WorkerManager
.
postWorker
Message
(
id
,
message
));
this
.
id
.
then
(
id
=>
ThreadManager
.
postThread
Message
(
id
,
message
));
}
}
terminate
()
{
terminate
()
{
this
.
id
.
then
(
WorkerManager
.
stopWorker
);
this
.
id
.
then
(
ThreadManager
.
stopThread
);
}
}
}
}
js/self.js
View file @
30000648
...
@@ -3,18 +3,18 @@ import {
...
@@ -3,18 +3,18 @@ import {
DeviceEventEmitter
,
DeviceEventEmitter
,
}
from
'
react-native
'
;
}
from
'
react-native
'
;
const
{
Worker
SelfManager
}
=
NativeModules
;
const
{
Thread
SelfManager
}
=
NativeModules
;
const
self
=
{
const
self
=
{
onmessage
:
null
,
onmessage
:
null
,
postMessage
:
(
message
)
=>
{
postMessage
:
(
message
)
=>
{
if
(
!
message
)
{
return
;
}
if
(
!
message
)
{
return
;
}
Worker
SelfManager
.
postMessage
(
message
);
Thread
SelfManager
.
postMessage
(
message
);
}
}
};
};
DeviceEventEmitter
.
addListener
(
"
Worker
Message
"
,
(
message
)
=>
{
DeviceEventEmitter
.
addListener
(
"
Thread
Message
"
,
(
message
)
=>
{
!!
message
&&
self
.
onmessage
&&
self
.
onmessage
(
message
);
!!
message
&&
self
.
onmessage
&&
self
.
onmessage
(
message
);
});
});
...
...
package.json
View file @
30000648
...
@@ -20,6 +20,5 @@
...
@@ -20,6 +20,5 @@
"peerDependencies"
:
{
"peerDependencies"
:
{
"react-native"
:
"^0.41.2"
,
"react-native"
:
"^0.41.2"
,
"react-native-windows"
:
"0.41.0-rc.1"
"react-native-windows"
:
"0.41.0-rc.1"
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment