Skip to content

Commit c1069c5

Browse files
authored
Merge pull request #327 from varungupta85/use-fixed-key
Use a hash function to compute key based on file name.
2 parents b911054 + 42f3295 commit c1069c5

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

sound.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,21 @@ var RNSound = require('react-native').NativeModules.RNSound;
44
var IsAndroid = RNSound.IsAndroid;
55
var IsWindows = RNSound.IsWindows;
66
var resolveAssetSource = require("react-native/Libraries/Image/resolveAssetSource");
7-
var nextKey = 0;
87

98
function isRelativePath(path) {
109
return !/^(\/|http(s?)|asset)/.test(path);
1110
}
1211

12+
// Hash function to compute key from the filename
13+
function djb2Code(str) {
14+
var hash = 5381, i, char;
15+
for (i = 0; i < str.length; i++) {
16+
char = str.charCodeAt(i);
17+
hash = ((hash << 5) + hash) + char; /* hash * 33 + c */
18+
}
19+
return hash;
20+
}
21+
1322
function Sound(filename, basePath, onError, options) {
1423
var asset = resolveAssetSource(filename);
1524
if (asset) {
@@ -24,7 +33,7 @@ function Sound(filename, basePath, onError, options) {
2433
}
2534

2635
this._loaded = false;
27-
this._key = nextKey++;
36+
this._key = djb2Code(filename);
2837
this._duration = -1;
2938
this._numberOfChannels = -1;
3039
this._volume = 1;

0 commit comments

Comments
 (0)